Peter Parker
Mitglied
Hallo an alle,
ich habe mir in VB.NET eine Klasse zusammengebaut, die mir aus einer XML Datei (mit 8000 Datensätze), Daten in einem Datagrid anzeigt. Funktioniert bestens. Wenn ich nun aber das ganze in einen Thread packe bekomme ich eine Fehlermeldung "There is no Sourcecode available for the current location" und als Exception:
Illegal cross-thread operation: Control 'DataGridView' accessed from a thread other than the thread it was created on.
Stack trace where the illegal operation occurred was:
at System.Windows.Forms.Control.get_Handle()
at System.Windows.Forms.Control.get_Focused()
at System.Windows.Forms.DataGridView.SetCurrentCellAddressCore(Int32, Int32, Boolean, Boolean, Boolean)
at System.Windows.Forms.DataGridView.MakeFirstDisplayedCellCurrentCell(Boolean)
at System.Windows.Forms.DataGridView.OnRowCollectionChanged_PostNotification(Boolean, Boolean, CollectionChangeAction)
at System.Windows.Forms.DataGridViewRowCollection.OnCollectionChanged_PostNotification(CollectionChangeAction, Int32, Int32, Boolean, Boolean, Boolean, Point)
at System.Windows.Forms.DataGridViewRowCollection.OnCollectionChanged(CollectionChangeEventArgs, Int32, Int32, Boolean, Boolean, Boolean, Point)
at System.Windows.Forms.DataGridViewRowCollection.InsertInternal(Int32, DataGridViewRow, Boolean)
at System.Windows.Forms.DataGridViewRowCollection.InsertInternal(Int32, DataGridViewRow)
at System.Windows.Forms.DataGridViewRowCollection.AddInternal(DataGridViewRow)
at System.Windows.Forms.DataGridVi..."
Was etwas komisch ist, wenn die XML Datei auf 1000 Datensätze kürze funktioniert alles bestens (mit der gleichen Funktion) !
Hier der Code:
Imports System.Threading
Imports System.Xml.XmlWriter
Imports System.Windows.Forms.DataGrid
Imports System.Data
Public Class oba_025
Dim thXML As New Thread(AddressOf leseXML)
Private Sub buttAuswstart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttAuswstart.Click
'Threads starten
thXML.Start() 'XML auslesen
End Sub
' Thread zum auslesen der XML Datei und deren Darstellung
Private Sub leseXML()
Dim ds As New DataSet("datensatz") 'Bezeichnung in xml Datei
Dim filePath As String 'Pfadangabe
filePath = "C:\dataset.xml"
'XML Daten auslesen
Try
ds.ReadXml(filePath)
With DataGridView
.DataSource = ds
.DataMember = "datensatz"
End With
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
End Class
ich habe mir in VB.NET eine Klasse zusammengebaut, die mir aus einer XML Datei (mit 8000 Datensätze), Daten in einem Datagrid anzeigt. Funktioniert bestens. Wenn ich nun aber das ganze in einen Thread packe bekomme ich eine Fehlermeldung "There is no Sourcecode available for the current location" und als Exception:
Illegal cross-thread operation: Control 'DataGridView' accessed from a thread other than the thread it was created on.
Stack trace where the illegal operation occurred was:
at System.Windows.Forms.Control.get_Handle()
at System.Windows.Forms.Control.get_Focused()
at System.Windows.Forms.DataGridView.SetCurrentCellAddressCore(Int32, Int32, Boolean, Boolean, Boolean)
at System.Windows.Forms.DataGridView.MakeFirstDisplayedCellCurrentCell(Boolean)
at System.Windows.Forms.DataGridView.OnRowCollectionChanged_PostNotification(Boolean, Boolean, CollectionChangeAction)
at System.Windows.Forms.DataGridViewRowCollection.OnCollectionChanged_PostNotification(CollectionChangeAction, Int32, Int32, Boolean, Boolean, Boolean, Point)
at System.Windows.Forms.DataGridViewRowCollection.OnCollectionChanged(CollectionChangeEventArgs, Int32, Int32, Boolean, Boolean, Boolean, Point)
at System.Windows.Forms.DataGridViewRowCollection.InsertInternal(Int32, DataGridViewRow, Boolean)
at System.Windows.Forms.DataGridViewRowCollection.InsertInternal(Int32, DataGridViewRow)
at System.Windows.Forms.DataGridViewRowCollection.AddInternal(DataGridViewRow)
at System.Windows.Forms.DataGridVi..."
Was etwas komisch ist, wenn die XML Datei auf 1000 Datensätze kürze funktioniert alles bestens (mit der gleichen Funktion) !
Hier der Code:
Imports System.Threading
Imports System.Xml.XmlWriter
Imports System.Windows.Forms.DataGrid
Imports System.Data
Public Class oba_025
Dim thXML As New Thread(AddressOf leseXML)
Private Sub buttAuswstart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttAuswstart.Click
'Threads starten
thXML.Start() 'XML auslesen
End Sub
' Thread zum auslesen der XML Datei und deren Darstellung
Private Sub leseXML()
Dim ds As New DataSet("datensatz") 'Bezeichnung in xml Datei
Dim filePath As String 'Pfadangabe
filePath = "C:\dataset.xml"
'XML Daten auslesen
Try
ds.ReadXml(filePath)
With DataGridView
.DataSource = ds
.DataMember = "datensatz"
End With
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
End Class