I want to create a Winforms app to read 48 text files, each with only one line of comma separated values and populate a MySQL database and DataGridView with the results from each.
From Trawling SO and Google and months of trial and error the best I've been able to come up with is the following:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
UpdateMcs()
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
Me.MainTableAdapter.Fill(Me.MyDataSet.main)
If stopPolling = False Then BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub UpdateMcs()
Dim tasks As New List(Of Task)
For Each row As DataRow In MyDataSet.main
If row("model") = "A" Then
tasks.Add(Task.Factory.StartNew(Sub()
Try
Dim Path As String = "\\" & row("server_ip") & "\mc_data\mc_" & Format(row("mc_num"), "00") & ".txt"
Dim lines() As String = File.ReadAllLines(Path)
Dim data As String = lines(0)
Dim state As New stateTableAdapter
Try
state.UpdateByIP(data, row("ip"))
Console.WriteLine("{0}: {1}", row("ip"), "Success")
Catch ex As Exception
Console.WriteLine("{0}: {1}", row("ip"), ex.Message)
End Try
Catch ex As Exception
Console.WriteLine("{0}: {1}", row("ip"), ex.Message)
End Try
End Sub))
End If
Next
Task.WaitAll(tasks.ToArray())
End Sub
What I'd like to do though is to have each machine update the DB and refresh UI as it is completed and not wait for the rest to complete.
I've tried doing this by spawning individual threads for each machine but it has resulted in very buggy behaviour and locked up UI etc. So not very successful.
I should also note that I have another 39 machines that will be polled for data using an API that will also need to be run individually.
Is there a more efficient way to tackle this problem?
One of the main issues is that if a machine (or a server hosting data file) is offline for some reason the time to establish that fact vs the time to poll a machine that is online could be 500ms vs 10secs so having all online machines waiting for the offline one means that the DB update frequency is severely limited by the worst performing poll.
Any advice on better techniques to use and resources for studying would be very welcome!!