У меня следующая структура:
OLD:
frmMain (WinForm)
uscStat (UserControl with Grid)
В frmMain я могу сделатьнекоторые настройки и загрузите результат настроек через LinQ в сетку uscStat.
fe:
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
Занимает около 30 секунд, в зависимости от пользовательских настроек.
NEW
Добавлено: modCommon (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
И я добавил следующий BackgroundWorker в modCommon :
Public thStat As Backgroundworker
Я инициализирую в 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
И создал следующие подпрограммы:
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
И изменил настройки подпрограмм, например:
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
В uscStat Я изменил / добавил следующее:
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
Это занимает около 5 минут при тех же пользовательских настройках, слишком медленно.
Как я могу улучшить это?