2014-04-01 40 views
-1

我的Windows窗体应用程序的工作: 在我的表格填写我在我的一些数据网格视图interval.so一段时间我的应用程序越来越stuck..so我使用回地面工作人员和回地面工人定时器
我打电话我的功能,填补了我的数据网格视图。 我设置定时器间隔为10000。在后台工作我给出这样的代码:后,,我怎么能解决

Private Sub BackgroundWorker1_DoWork 
Call Fetch_Info() 
End Sub 

在定时器点击事件中,我给出这样的代码thise:

If Not BackgroundWorker1.IsBusy Then 
      BackgroundWorker1.RunWorkerAsync() 
     End If 

我Fetch_Info()函数是这样的:

Dim cnt As Integer 
      Dim tbarcodedgv As String 
      Dim totaltbarcode As String 
      cnt = DGVall.RowCount 
Dim tbar As String 
      Dim locTable As New DataTable 
      locTable.Columns.Add("carid", GetType(String)) 
      If cnt > 0 Then 
       For i = 0 To cnt - 2 
        tbarcodedgv = DGVall.Rows(i).Cells(0).Value 
locTable.Rows.Add(tbarcodedgv) 
       Next 
      End If 
Dim flag As Boolean = False 
      Dim dcnt As Integer = DGVall.RowCount 
      Dim trid As Integer 
      Dim tbarcode As String 
      Dim keyloc As String 
      Dim cmd23 As New SqlCommand("IBS_fetchrequested", con.connect) 
      cmd23.CommandType = CommandType.StoredProcedure 
      cmd23.Parameters.Add("@tid", SqlDbType.Int).Value = tid 
      If cnt > 1 Then 
       Dim tvp1 As SqlParameter = cmd23.Parameters.AddWithValue("@Tbaroced", locTable) 
       tvp1.SqlDbType = SqlDbType.Structured 
       tvp1.TypeName = "dbo.TBarcode" 
      End If 


      dr = cmd23.ExecuteReader 
      While dr.Read 
       flag = False 
       tbarcode = dr("TBarcode") 
If flag = False Then 
        If dr("keyloc") Is DBNull.Value Then 
         keyloc = "" 
        Else 
         keyloc = dr("keyloc") 
        End If 

        Dim row0 As String() = {tbarcode, keyloc, "", "Release"} 
        DGVall.Rows.Add(row0) 
        AxWindowsMediaPlayer1.URL = "C:\Beep.mp3" 
       End If 
      End While 
      dr.Close() 
      con.disconnect() 

回答

1

当你后台工作者在另一个线程中运行,而不是在GUI中操作GUI线程中运行的Datagridview。这应该通常不起作用,但这可能是为什么在BGW运行时GUI挂起的原因。

尝试分割工作:在Backgroundworker的DoWork事件处理程序中执行耗时从数据库获取数据的时间,并将结果设置为DoWork函数中EventArgs变量的e.Result值。

然后,您处理Backgroundworker的RunWorkerCompleted事件,您可以使用在DoWork方法中设置的结果快速更新您的datagridview。这样,你的GUI无关与实际耗时的任务,只能由您的datagridview的快速更新的影响。

这样做的代码示例:

Public Class Form1 
Private WithEvents LazyBGW As New System.ComponentModel.BackgroundWorker 

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
    'This code runs in the UI-Thread 
    LazyBGW.RunWorkerAsync() 
End Sub 

Private Sub LazyBGW_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles LazyBGW.DoWork 
    'This code runs in the BGW-Thread 
    Dim a As Integer = 0 
    For i = 1 To 5 
     a += 1 
     'I'm a lazy worker, so after this hard work I need to... 
     Threading.Thread.Sleep(1000) 'This locks up the BGW-Thread, not the UI-thread 
    Next 
    'Work is done, put results in the eventargs-variable for further processing 
    e.Result = a 
End Sub 

Private Sub LazyBGW_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles LazyBGW.RunWorkerCompleted 
    'This code runs in the UI-Thread 
    Dim results As Integer = CInt(e.Result) 'e.Result contains whatever you put into it in the DoWork() method 
    MessageBox.Show("Finally the worker is done and our result is: " & results.ToString) 
End Sub 
End Class 
+0

我没有得到你想say..any代码,我不得不改变 – user3106114

+0

这是关于什么的线程。一个线程一次只能做一件事。你的表单在一个线程中运行,所以如果你在这个线程中做了一些冗长的事情,你的表单不会回应。这就是为什么您使用在并行线程中运行的BGW,因此表单不受其影响。但是,如果这个BGW线程不断地与你的UI线程混淆,你的表单仍然锁定。所以你需要将它们完全分开。我将添加一个工作流程的代码示例。 – Jens

+0

好吧,先生,现在我明白了,,,, – user3106114

相关问题