1

通常我自己处理我的错误,但这次我需要专家的帮助!这从来没有发生在我身上,一个人拥有的数据越少(通常)你能说出发生的事情就越少。

我正在尝试编写一个简单的查询分析器。我随机收到这些类型的崩溃:

1)我从以下功能开始:

Dim thd As New Thread(AddressOf StartSub)
thd.Start()

然后 Startsub 如下:

 Public Sub StartSub()
    CheckForIllegalCrossThreadCalls = False
    txtExecution.Text = "Executing query..."
    Dim query As String = QueryBuilder()
    UpdateView(query)
End Sub

然后 updateview 更新我拥有的数据网格:

    Dim da As New SqlCeDataAdapter(query, connStr)
    Dim dt As New DataTable()
    Try
        da.Fill(dt)
        txtExecution.Text = "Query executed successfully."
        dgTickets.DataSource = dt
    Catch ex As Exception
        txtExecution.Text = "Query failed."
        tbGrid.BeginInvoke(Sub() tbGrid.SelectedTab = tbGrid.TabPages(1))
    End Try

2) UpdateQuery 中的代码在以下行崩溃(调试器并没有说它在这里崩溃,我通过选择所有行并逐个检查它来猜测它):

dgTickets.DataSource = dt

3) 调试器所说的: NullReferenceException 未处理(...) 使用 new 关键字创建对象实例

堆栈跟踪:

at System.Windows.Forms.DataGridViewCell.GetEditedFormattedValue(Object value, Int32 rowIndex, DataGridViewCellStyle& dataGridViewCellStyle, DataGridViewDataErrorContexts context)
   at System.Windows.Forms.DataGridViewCell.PaintWork(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, Int32 rowIndex, DataGridViewElementStates cellState, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
   at System.Windows.Forms.DataGridViewRow.PaintCells(Graphics graphics, Rectangle clipBounds, Rectangle rowBounds, Int32 rowIndex, DataGridViewElementStates rowState, Boolean isFirstDisplayedRow, Boolean isLastVisibleRow, DataGridViewPaintParts paintParts)
   at System.Windows.Forms.DataGridViewRow.Paint(Graphics graphics, Rectangle clipBounds, Rectangle rowBounds, Int32 rowIndex, DataGridViewElementStates rowState, Boolean isFirstDisplayedRow, Boolean isLastVisibleRow)
   at System.Windows.Forms.DataGridView.PaintRows(Graphics g, Rectangle boundingRect, Rectangle clipRect, Boolean singleHorizontalBorderAdded)
   at System.Windows.Forms.DataGridView.PaintGrid(Graphics g, Rectangle gridBounds, Rectangle clipRect, Boolean singleVerticalBorderAdded, Boolean singleHorizontalBorderAdded)
   at System.Windows.Forms.DataGridView.OnPaint(PaintEventArgs e)
   at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer)
   at System.Windows.Forms.Control.WmPaint(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.DataGridView.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.Run(ApplicationContext context)
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
   at SQLquery.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81 

这实际上是很模糊的。上面指定的文件不存在。它崩溃的地方是用 Try-End Try 包裹的。此外,是的,我已经设置了绘画活动,但它不应该涉及它(或者它可能涉及?)。

就这一点而言,我将非常感谢任何提示。我必须补充一点,我使用的是 Visual Basic Express Edition。该错误偶尔会发生 - 有时当我很幸运时什么都没有发生,而当我没有发生时,我会遇到这个崩溃。

皮特。

4

1 回答 1

3

您永远不应该触摸/更新后台线程内的任何 GUI 控件。所以像这样的行:

txtExecution.Text = "Executing query..."

dgTickets.DataSource = dt

后台线程内部注定要失败。这应该始终在主 GUI 线程上使用Control.BeginInvoke.

您似乎正确执行的唯一 GUI 更新tbGridcatch

tbGrid.BeginInvoke(Sub() tbGrid.SelectedTab = tbGrid.TabPages(1))

您应该阅读有关UI WinForms 线程调用的信息。

于 2011-09-19T21:33:41.593 回答