2012-01-03 122 views
0

我有以下結構:
OLD:
frmMain(WinForm的)
uscStat(用戶控件與電網)Backgroundworker非常慢,如何優化?

在frmMain我能夠做一些設置,並通過加載的設置的結果LinQ進入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 

注意到輪約30秒,這取決於用戶的設置。

NEW
加了:modCommon(模塊)
通過所有添加以下類的模塊中的變量:

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 

我初始化爲frmMa在 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分鐘,太慢。
我該如何改進?

+1

ReportProgress價格昂貴,不要爲每個行調用它。如果沒有幫助,請使用分析器。 – 2012-01-03 16:13:58

+0

Hmmm ReportProgress被稱爲100次(1%,2%,...),而不是每行。這仍然太多了嗎? – Xennon 2012-01-04 07:57:23

回答

0

如果我理解正確的,你複製一個數據表到另一個數據表和應用FuncXY到列。我不知道FuncXY做了什麼。用SQL查詢可以做到這一點嗎?這比循環單個記錄要快得多。

+0

我不認爲我複製了Datatable,因爲我將該變量作爲參考。糾正我我錯了。將FuncXY直接放入SQL中?我會先說:不。但我會考慮一下。但實際上,它運行時沒有Backgroundworker相對較快。 – Xennon 2012-01-04 08:03:02