Backgroundworker很慢,如何优化?

时间:2012-01-03 15:43:20

标签: vb.net linq delegates backgroundworker invoke

我有以下结构:
OLD:
frmMain(WinForm)
uscStat(带网格的UserControl)

在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秒,具体取决于用户设置。


补充: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

我在 modCommon 中添加了以下BackgroundWorker:

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

并将settings-subs更改为:

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 个答案:

答案 0 :(得分:0)

如果我理解正确,您将一个DataTable复制到另一个DataTable并将FuncXY应用于列。我不知道FuncXY做了什么。是否可以使用SQL查询执行此操作?它比循环浏览单个记录要快得多。