Backgroundworker is very slow, how to optimize? - vb.net

I have the following structure:
OLD:
frmMain (WinForm)
uscStat (UserControl with Grid)
In frmMain I'm able to make some settings and load the result of the settings via LinQ into the Grid of the uscStat.
f.e.:
frmMain
Private Sub rdbValue2_ValueChanged(sender As System.Object, e As System.EventArgs) Handles rdbValue2.ValueChanged
uscStat.Load(Value1,Value2,Value3,Value4, ...)
End Sub
uscStat
Public Sub Load(ByVal Value1, ByVal Value2, ByVal Value3, ...)
...
Dim dt As New Datatable
dt.Columns.Add(New DataColumn("Value1", GetType(Double)
...
Dim res As IEnumerable = ReturnDatable(Value2, Value3, ...)
Dim rowObj(6) As Object
...
For each item in res
rowObj(1) = FuncXY(item.Value1)
rowObj(2) = item.Value2
...
dt.Rows.Add(rowObj)
Next
dg.Datasource = dt
End Sub
Takes round about 30 sec, depending on the User-Settings.
NEW
Added: modCommon (Module)
To pass all the variables I added the following Class in the module:
Public Class clsStatValues
Public Property Value1() As Boolean
Get
Return m_Value1
End Get
Set(value As Boolean)
m_Value1 = value
End Set
End Property
Private m_Value1 As Boolean
Public Property Value2() As Integer
Get
Return m_Value2
End Get
Set(value As Integer)
m_Value2 = value
End Set
End Property
Private m_Value2 As Integer
...
End Class
And I add the following BackgroundWorker in modCommon:
Public thStat As Backgroundworker
I initialize in frmMain thStat:
thStat = New BackgroundWorker
AddHandler thStat.DoWork, AddressOf thStat_DoWork
AddHandler thStat.RunWorkerCompleted, AddressOf thStat_Completed
AddHandler thStat.ProgressChanged, AddressOf thStat_ProgressChanged
thStat.WorkerReportsProgress = True
thStat.WorkerSupportsCancellation = True
And created the following subs:
Private Sub thStat_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)
Dim varStat As clsStatValues = e.Argument
uscStat.Load(varStat.Value1, varStat.Value2, ...)
End Sub
And changed the settings-subs like:
Private Sub rdbValue2_ValueChanged(sender As System.Object, e As System.EventArgs) Handles rdbValue2.ValueChanged
Dim varStat As New clsStatValues
varStat.Value1 = True
varStat.Value2 ...
...
thStat.RunWorkerAsync(varStat)
End Sub
In uscStat I changed/added the following:
Public Sub Load(ByVal Value1, ...)
...
Me.Invoke(New AddDataSourceToGrid(AddressOf AddDataSourceToGridFunction), GetDatatable(Value1, Value2, ...))
...
End Sub
Delegate Sub AddDataSourceToGrid(ByRef tmpDt As DataTable)
Private Sub AddDataSourceToGridFunction(ByRef tmpDt As DataTable)
dg.DataSource = tmpDt
End Sub
Public Function GetDatatable(ByVal Value1, ByVal Value2, ByVal Value3, ...) As Datatable
...
Dim dt As New Datatable
dt.Columns.Add(New DataColumn("Value1", GetType(Double)
...
Dim res As IEnumerable = ReturnDatable(Value2, Value3, ...)
Dim resultCount = res.AsQueryable.Count
Dim ReportEvery As Double = resultCount/100
Dim staticReportEvery As Double = ReportEvery
Dim count As Integer = 0
Dim Percent As Integer = 0
Dim rowObj(6) As Object
...
For each item in res
count += 1
If count > ReportEvery then
Percent += 1
thStat.ReportProgress(Percent, count & " of " & resultCount)
ReportEvery += staticReportEvery
End If
rowObj(1) = FuncXY(item.Value1)
rowObj(2) = item.Value2
...
dt.Rows.Add(rowObj)
Next
Return dt
End Sub
This takes round about 5 mins with the same user-settings, too slow.
How can I improve it?

If I understand right, you copy one DataTable into another DataTable and apply FuncXY to a column. I do not know what FuncXY does. Would it be possible to do that with an SQL query? It would be much faster than looping through individual records.

Related

How do i get specific columns from datatables to fill a constructor with?

I got 2 DataTables which i open with a openFileDialog with the following columnnames: "UANR", "KostenArt", "Ueberbegriff", "Benennung", "Anzahl", "Einheit", "Einzelkosten", "SummenCode", "Kst", "AufPos", "Summenkosten".
But i only need the values for the following ones,
ReadOnly Property KostenArt As String
ReadOnly Property UANR As String
ReadOnly Property Ueberbegriff As String
ReadOnly Property Benennung As String
ReadOnly Property Anzahl As Double
ReadOnly Property Einheit As String
ReadOnly Property Einzelkosten As Double
ReadOnly Property Gesamtmenge As Integer
ReadOnly Property Summencode As String
to fill the constructor with and make a list of objects with this method:
Private Function ConvertDataTableToListOfISAACService(dt As DataTable, lst As List(Of ISAACService)) As List(Of ISAACService)
For Each row As DataRow In dt.Rows
Dim ISAAC As New ISAACService(row(KostenArt).ToString, row(UANR).ToString, row(Ueberbegriff).ToString, row(Benennung).ToString, CDbl(row(Anzahl)), row(Einheit).ToString, CDbl(row(Einzelkosten)), CInt(row(Gesamtmenge)), row(Summencode).ToString)
lst.Add(ISAAC)
Next
Return lst
End Function
Here is how i put in the datatables:
Private Sub btnDatei1_Click(sender As Object, e As EventArgs) Handles btnDatei1.Click
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
If File.Exists(OpenFileDialog1.FileName) Then
dt1 = FileGenerator.ReadFromProtectedFile(OpenFileDialog1.FileName)
dgv1.DataSource = dt1
ConvertDataTableToListOfISAACService(dt1, lst1)
End If
End If
End Sub
Private Sub btnDatei2_Click(sender As Object, e As EventArgs) Handles btnDatei2.Click
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
dt2 = FileGenerator.ReadFromProtectedFile(OpenFileDialog1.FileName)
dgv2.DataSource = dt2
ConvertDataTableToListOfISAACService(dt2, lst2)
End If
End Sub
The full code:
Public Class MainForm
Private Const KostenArt As String = "KostenArt"
Private Const UANR As String = "UANR"
Private Const Ueberbegriff As String = "Ueberbegriff"
Private Const Benennung As String = "Benennung"
Private Const Anzahl As String = "Anzahl"
Private Const Einheit As String = "Einheit"
Private Const Einzelkosten As String = "Einzelkosten"
Private Const Gesamtmenge As String = "Gesamtmenge"
Private Const Summencode As String = "Summencode"
Public dt1, dt2 As DataTable
Public lst1, lst2 As List(Of ISAACService)
Private Sub btnDatei1_Click(sender As Object, e As EventArgs) Handles btnDatei1.Click
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
If File.Exists(OpenFileDialog1.FileName) Then
dt1 = FileGenerator.ReadFromProtectedFile(OpenFileDialog1.FileName)
dgv1.DataSource = dt1
ConvertDataTableToListOfISAACService(dt1, lst1)
End If
End If
End Sub
Private Sub btnVergleich_Click(sender As Object, e As EventArgs) Handles btnVergleich.Click
'CompareDataTables()
End Sub
Private Sub btnDatei2_Click(sender As Object, e As EventArgs) Handles btnDatei2.Click
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
dt2 = FileGenerator.ReadFromProtectedFile(OpenFileDialog1.FileName)
dgv2.DataSource = dt2
ConvertDataTableToListOfISAACService(dt2, lst2)
End If
End Sub
Private Sub CompareDataTables(ByRef lst1 As List(Of ISAACService), ByRef lst2 As List(Of ISAACService))
End Sub
Private Function ConvertDataTableToListOfISAACService(dt As DataTable, lst As List(Of ISAACService)) As List(Of ISAACService)
Try
For Each row As DataRow In dt.Rows
Dim ISAAC As New ISAACService(row(KostenArt).ToString, row(UANR).ToString, row(Ueberbegriff).ToString, row(Benennung).ToString, CDbl(row(Anzahl)), row(Einheit).ToString, CDbl(row(Einzelkosten)), CInt(row(Gesamtmenge)), row(Summencode).ToString)
lst.Add(ISAAC)
Next
Return lst
Catch ex As System.ArgumentException
End Try
End Function
End Class
Think the problem is in the way you've declared lst and the signature of the ConvertDataTableToListOfISAACService method. Try something like this maybe.
Private Function ConvertDataTableToListOfISAACService(dt As DataTable) As List(Of ISAACService)
Dim lst As Zew List(Of ISAACService)
Try
For Each row As DataRow In dt.Rows
Dim ISAAC As New ISAACService(row(KostenArt).ToString, row(UANR).ToString, row(Ueberbegriff).ToString, row(Benennung).ToString, CDbl(row(Anzahl)), row(Einheit).ToString, CDbl(row(Einzelkosten)), CInt(row(Gesamtmenge)), row(Summencode).ToString)
lst.Add(ISAAC)
Next
Return lst
Catch ex As System.ArgumentException
'Do something with the exception, here.
End Try
End Function
It's also more than possible something is throwing an exception, but with your empty catch block, it's not being reported

Vb.net bindingsource.filter from a list of strings

i have in a windows.form a combobox and a datagridview,
i add rows in datagridview with a button and get the value(string) from combobox and if i double click on the row of datagridview i delete the row.
When i add the row i want to hide/disable/remove the combobox value and when i delete the row i want to restore it in combobox.
The combobox values are binding from dataset source and combobox is dropdownlist style.
I try some things until now but this is what i think is better approach and where i am:
Dim filterList As List(Of String) = New List(Of String)
Private Sub filterListAdd()
Dim dgResult As String
filterList .Clear() 'Clear the list so no duplicates
For i As Integer = 0 To combobox.Items.Count - 1
Dim a As String = combobox.GetItemText(combobox.Items(i))
For row As Integer = 0 To Dgview.RowCount - 1
For col As Integer = 0 To Dgview.ColumnCount - 1
Next
Surname = Dgview.Rows(row).Cells(0).Value.ToString
If dgResult = a Then
filterList .Add(a) 'Add to list
End If
Next
Next i
End Sub
Private Sub Button_Click(sender As Object, e As EventArgs) Handles Button.Click
Dgview.Rows.Add(combobox.Text)
filterListAdd()
'Here i want to bindingsource.filter = filterList
End Sub
Private Sub Dgview_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles Dgview.MouseDoubleClick
Dgview.Rows.Remove(Dgview.CurrentRow)
'I Guess here with the same way i filter it again
End Sub
Any help will be appreciated, thanks in advance.
Panos
Public Class Form1
Dim clsItems As New ArrayList()
Private Sub RemoveComboItem()
Me.BindingSource.Position = Me.BindingSource.Find("1", Combobox.Text)
Dim 1Add As String = DirectCast(BindingSource.Current, DataRowView).Item("1").ToString
Dim 2Add As String = DirectCast(BindingSource.Current, DataRowView).Item("2").ToString
Dim 3Add As String = DirectCast(BindingSource.Current, DataRowView).Item("3").ToString
clsItems.Add(New MyItem(1Add, 2Add, 3Add))
BindingSource.RemoveCurrent()
End Sub
Private Sub AddComboItem(dg As DataGridView) ' Because i have two datagridviews
Dim 1 As String = dg.CurrentRow.Cells(0).Value.ToString()
For i = 0 To clsItems.Count - 1
If i <= clsItems.Count - 1 Then
If 1 = clsItems(i).1 Then
Dim drNewRow As DataRow
drNewRow = DeeDataSet.Tables("Table").NewRow()
drNewRow("1") = clsItems(i).1
drNewRow("2") = clsItems(i).2
drNewRow("3") = clsItems(i).3
DeeDataSet.Tables("Table").Rows.Add(drNewRow)
clsItems.Remove(clsItems(i)) ' Remove it from array so no duplicates
End If
End If
Next
End Sub
Private Sub Button_Click(sender As Object, e As EventArgs) Handles Button.Click
RemoveComboItem()
End Sub
Private Sub Dgview_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles Dgview.MouseDoubleClick
AddComboItem(Dgview)
Dgview.Rows.Remove(Dgview.CurrentRow)
End Sub
End class
Public Class MyItem
Private m_s1 As String
Private m_s2 As String
Private m_s3 As String
Public Sub New(1As String, 2 As String, 3 As String)
M_sName = 1
M_sSurname = 2
M_sTitle = 3
End Sub
Public ReadOnly Property 1
Get
Return m_s1
End Get
End Property
Public ReadOnly Property 2
Get
Return M_s2
End Get
End Property
Public ReadOnly Property 3
Get
Return M_s3
End Get
End Property
End Class
In that form i don't update the dataset because every change i want to make it in other form, so when i finish from here the dataset.table has no changes.

Variable not setting or for loop not working correctly

i'm having an issue with setting a variable to the return value of a function i made i can't seem to be able to get it to work and i get no errors or feedback from visual studios even in strict mode
Imports System.Net
Imports System.IO
Imports System.ComponentModel
Public Class Form2
Dim i As Integer
Public CleanSearchTexts As String()
Dim count As Integer = 0
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If BackgroundWorker1.IsBusy = False Then
i = RichTextBox1.Lines.Count
i = i - 1
BackgroundWorker1.RunWorkerAsync()
Else
MsgBox("Threads are busy")
End If
End Sub
Private Sub StartThreads()
Dim SearchText As String
For count = count To i
SearchText = LineFunc(count)
count += 1
SearchText = CType(Ask_Query(SearchText), String)
SearchText = CType(Bing_Query(SearchText), String)
SearchText = CType(Yahoo_Query(SearchText), String)
Dim thread_count As String = CType(Process.GetCurrentProcess().Threads.Count - 20, String)
Label_T(thread_count)
Threading.Thread.Sleep(500)
If SearchText.Contains("All_Query:Yes") Then
SearchText = SearchText.Replace("All_Query:Yes", "")
RTB(SearchText)
End If
Next
End Sub
Private Delegate Sub UpdateStatus(ByVal s As String)
Private Delegate Sub UpdateLabel(ByVal thread_count As String)
Public Delegate Function GetTextBox(ByVal index As Integer) As String
Public Function LineFunc(ByVal index As Integer) As String
If InvokeRequired Then
Invoke(New GetTextBox(AddressOf LineFunc), index)
Else
Dim indexSearchText As String
indexSearchText = RichTextBox1.Lines(index)
Return indexSearchText
End If
End Function
Public Sub RTB(ByVal s As String)
If Me.InvokeRequired Then
Me.Invoke(New UpdateStatus(AddressOf RTB), New Object() {s})
Else
RichTextBox2.AppendText(Environment.NewLine & s)
End If
End Sub
Public Sub Label_T(ByVal thread_count As String)
If Me.InvokeRequired Then
Me.Invoke(New UpdateLabel(AddressOf Label_T), New Object() {thread_count})
Else
Label3.Text = "Threads Running: " + thread_count
End If
End Sub
Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
BackgroundWorker1.WorkerSupportsCancellation = True
BackgroundWorker1.WorkerReportsProgress = True
Dim count As Integer
Dim num As Integer = CInt(TextBox1.Text) - 1
For count = 0 To num
Dim thread = New Threading.Thread(AddressOf StartThreads)
thread.IsBackground = True
thread.Name = "Web Thread #" + CType(count, String)
thread.Start()
Threading.Thread.Sleep(500)
Next
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
MsgBox("Work is done")
End Sub
End Class
the issue is clearly with either the line getting function or the for count
i have tested the function by adding a button and doing the process of setting a variable to the return and msgboxing it and it works perfectly not only that when i breakpoint it it shows it's returning the right output but its just not setting it for whatever reason in this sub
Public Sub StartThreads(ByVal List As Object)
Dim List2 As String() = CType(List, String())
For Each dork As String In List2
dork = CType(Ask_Query(dork), String)
dork = CType(Bing_Query(dork), String)
dork = CType(Yahoo_Query(dork), String)
Dim thread_count As String = CType(Process.GetCurrentProcess().Threads.Count - 20, String)
Label_T(thread_count)
Threading.Thread.Sleep(500)
If dork.Contains("All_Query:Yes") Then
dork = dork.Replace("All_Query:Yes", "")
RTB(dork)
End If
Next
End Sub
and here is the function
Public Delegate Function GetTextBox(ByVal index As Integer) As String
Public Function LineFunc(ByVal index As Integer) As String
If InvokeRequired Then
Invoke(New GetTextBox(AddressOf LineFunc), index)
Else
Dim indexSearchText As String
indexSearchText = RichTextBox1.Lines(index)
Return indexSearchText
End If
End Function
Update: List Split Func
Public Function splitData(ByVal width As Integer, ByVal dd As List(Of String)) As List(Of List(Of String))
Dim dds As New List(Of List(Of String))
Dim numberOfLists As Integer = (dd.Count \ width)
For i As Integer = 0 To numberOfLists
Dim newdd As List(Of String)
newdd = dd.Skip(i * width).Take(width).ToList()
dds.Add(newdd)
Next i
Return dds
End Function
Updated Background Worker
Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
BackgroundWorker1.WorkerSupportsCancellation = True
BackgroundWorker1.WorkerReportsProgress = True
Dim dds As New List(Of String)
GetList(dds)
Dim num As Integer = CInt(TextBox1.Text)
Dim ThreadCount As Integer = CInt(TextBox1.Text)
If ThreadCount > 1 Then
ThreadCount -= 1
End If
Dim RTBLines As Integer = RTB_Lines()
num = CInt(RTBLines / num)
num = CInt(Math.Ceiling(num))
Dim splitdd As List(Of List(Of String)) = CType(splitData(num, dds), List(Of List(Of String)))
For count = 0 To ThreadCount
Dim ListArray As String() = splitdd(count).ToArray
Dim newthread As New Thread(AddressOf StartThreads)
newthread.Name = "Web Thread #" + CType(count, String)
newthread.Start(ListArray)
Threading.Thread.Sleep(500)
Next
End Sub

Multithreading duplicate error after background worker added

I wrote a program that does queries on some search engines that I wanted to be multithreaded so it could do the searches faster. I made a question about this which was answered indirectly, but I have yet another problem even after I set up delegates and such.
I was running the queries on the main thread, so I had to set up a BackgroundWorker. I did that but even though everything seems to run without an issue, I get duplicates of my results even though the program is suppose to do the opposite. Sometimes it doesn't even read or output a result that I know would be good.
Imports System.Net
Imports System.IO
Imports System.ComponentModel
Public Class Form2
Dim i As Integer
Dim SearchString As String
Public CleanSearchStrings As String()
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
i = RichTextBox1.Lines.Count
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub StartThreads()
While i > 0
i = i - 1
Dim thread_count As String = Process.GetCurrentProcess().Threads.Count - 20
Label_T(thread_count)
SearchString = LineFunc(i)
Threading.Thread.Sleep(500)
SearchString = Ask_Query(SearchString) #This was commented out
SearchString = Bing_Query(SearchString) #just simple webscraping
SearchString = Yahoo_Query(SearchString)
If SearchString.Contains("All_Query:Yes") Then
SearchString = SearchString.Replace("All_Query:Yes", "")
RTB(SearchString)
End If
End While
End Sub
Private Delegate Sub UpdateStatus(ByVal s As String)
Private Delegate Sub UpdateLabel(ByVal thread_count As String)
Private Delegate Function Line(ByVal i As Integer)
Function LineFunc(ByVal i As Integer)
If Me.InvokeRequired Then
Me.Invoke(New Line(AddressOf LineFunc), New Object() {i})
Else
SearchString = RichTextBox1.Lines(i).ToString
Return SearchString
End If
End Function
Sub RTB(ByVal s As String)
If Me.InvokeRequired Then
Me.Invoke(New UpdateStatus(AddressOf RTB), New Object() {s})
Else
RichTextBox2.AppendText(Environment.NewLine & s)
End If
End Sub
Sub Label_T(ByVal thread_count As String)
If Me.InvokeRequired Then
Me.Invoke(New UpdateLabel(AddressOf Label_T), New Object() {thread_count})
Else
Label3.Text = "Threads Running: " + thread_count
End If
End Sub
Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
BackgroundWorker1.WorkerSupportsCancellation = True
BackgroundWorker1.WorkerReportsProgress = True
Dim count As Integer
Dim num As Integer = TextBox1.Text - 1
For count = 0 To num
Dim thread = New Threading.Thread(AddressOf StartThreads)
thread.IsBackground = True
thread.Start()
Threading.Thread.Sleep(500)
Next
End Sub
End Class
I know it has something to do with the LineFunc that I wrote but I can't seem to figure it out.
This was working flawlessly on multithreading, but the UI would be frozen and after I added the BackgroundWorker it seems to be giving me these duplicate results error and not reading the TextBox fully.
Update:
Private Sub StartThreads()
For count = count To i
Dim SearchString As String = LineFunc(count)
count += 1
Dim thread_count As String = CType(Process.GetCurrentProcess().Threads.Count - 20, String)
Label_T(thread_count)
Threading.Thread.Sleep(500)
SearchString = CType(Ask_Query(SearchString), String)
SearchString = CType(Bing_Query(SearchString), String)
SearchString = CType(Yahoo_Query(SearchString), String)
If SearchString.Contains("All_Query:Yes") Then
SearchString = SearchString.Replace("All_Query:Yes", "")
RTB(SearchString)
End If
Next
End Sub
Heres my Revised Get Line Func
Private Delegate Function GetTextBox(ByVal index As Integer) As String
Public Function LineFunc(ByVal index As Integer) As String
If Me.InvokeRequired Then
Me.Invoke(New GetTextBox(AddressOf LineFunc), New Object() {index})
Else
Dim indexSearchString As String
indexSearchString = CType(RichTextBox1.Lines(index), String)
Return indexSearchString
End If
End Function
If i use the a msgbox it gives me the right values i don't understand how come it does not give me the right values when i do the func Dim SearchString As String = LineFunc(count) it returns a null string in the end i don't understand why
this seemed to work for the duplicate issue but now i seem to be having issues with my output to richtextbox2 only outputing blanks and the line func is only giving out blanks i don't understand why either but atleast there is progress
I can't verify if this will work as you haven't posted the search functions, but here goes.
Public Class Form2
Dim i As Integer
Dim SearchString As String
Dim ResultString As String
Public CleanSearchStrings As String()
Private threadCount As Integer
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
SearchUsingRTB1Items()
End Sub
Private Sub SearchUsingRTB1Items()
threadCount = 0
Dim rtb1Lines() As String = RichTextBox1.Lines
Parallel.ForEach(rtb1Lines, Sub(line As String)
dim ResultString As String =""
threadCount += 1
Label_T(i.ToString)
ResultString =Ask_Query(line) 'This was commented out
ResultString = ResultString & Bing_Query(line) 'just simple webscraping
ResultString = ResultString & Yahoo_Query(line)
If ResultString.Contains("All_Query:Yes") Then
ResultString = ResultString.Replace("All_Query:Yes", "")
RTB(ResultString)
End If
threadCount -= 1
Label_T(i.ToString)
End Sub)
End Sub
Private Delegate Sub UpdateStatus(ByVal s As String)
Private Delegate Sub UpdateLabel(ByVal thread_count As String)
Sub RTB(ByVal s As String)
If Me.InvokeRequired Then
Me.Invoke(New UpdateStatus(AddressOf RTB), New Object() {s})
Else
RichTextBox2.AppendText(Environment.NewLine & s)
End If
End Sub
Sub Label_T(ByVal thread_count As String)
If Me.InvokeRequired Then
Me.Invoke(New UpdateLabel(AddressOf Label_T), New Object() {thread_count})
Else
Label3.Text = "Threads Running: " & thread_count
End If
End Sub
End Class

CSV file to datagridview using list

This is similar to:
https://social.msdn.microsoft.com/Forums/en-US/7b0e28b4-c1ef-49d6-8f46-11b379428052/import-from-csv-file-to-two-dimensional-array?forum=vbgeneral
but the code that Cor Ligthert suggests doesn't work even though its exactly what I need.
The code is:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim FILE_NAME As String = "C:\Users\Admin\Documents\test.csv"
Dim OutlookLines As New List(Of OutlookLine)
Dim sr As New IO.StreamReader(FILE_NAME)
Do Until sr.EndOfStream
OutlookLines.Add(New OutlookLine With {.line = sr.ReadLine})
Loop
DataGridView1.DataSource = OutlookLines
End Sub
Private Class OutlookLine
Private theLine As String
Public Property line() As String
Get
Return theLine
End Get
Set(ByVal value As String)
theLine = value
End Set
End Property
End Class
End Class
I try putting in:
.Split(","c)
after sr.ReadLine but it says "Value of type 1-D array of String cannot be converted to String"
The code above works fine but each row of the csv is scrunched into one column (obviously because I am not splitting it at the ",").
csv data:
1,2,3,4,5
6,7,8,9,0
See if this works for you:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
DataGridView1.DataSource = CSVToDataTable("C:\Users\Admin\Documents\test.csv", True)
End Sub
Private Function CSVToDataTable(filePath As String, Optional hasHeaderRow As Boolean = False) As DataTable
Dim rows = IO.File.ReadAllLines(filePath).Select(Function(l) l.Split(","c))
Dim dt = New DataTable
Dim count = 0
dt.Columns.AddRange(rows.First.Select(Function(c) New DataColumn With {.ColumnName = If(hasHeaderRow, c, "Column" & Threading.Interlocked.Increment(count))}).ToArray())
For Each row In rows.Skip(If(hasHeaderRow, 1, 0))
Dim dr = dt.NewRow()
dr.ItemArray = row
dt.Rows.Add(dr)
Next
Return dt
End Function
If you wish for the line property of the OutlookLine class to be an array of string, you should define it like this:
Private Class OutlookLine
Private theLine As String()
Public Property line As String()
Get
Return theLine
End Get
Set(ByVal value As String())
theLine = value
End Set
End Property
End Class
Then this will work:
OutlookLines.Add(New OutlookLine With {.line = sr.ReadLine.Split(",")})