2012-09-15 2 views
0

이 오류가 나타납니다.크로스 스레드 작업이 유효하지 않습니다. 컨트롤 'listview1'내가 진행률 표시 줄에서 사용하는 스레드 이외의 스레드에서 access

1000 개가 넘는 레코드가있는 데이터베이스 삽입 방법을 처리하는 진행률 표시 줄을 처리하려고했습니다. 스레드와 동일한 프로세스를로드하려면 진행률 막대가 필요합니다.

오류가 있습니다. 교차 스레딩 작업이 유효하지 않습니다. 생성 된 스레드가 아닌 다른 스레드에서 액세스하는 'listview1'제어.

Imports System.IO 
Public Class Form1 
    Private WithEvents DT As New DataTable 
    Private Delegate Sub AsyncDelegate(ByVal value As Integer) 
    Private ProgressUpdater As New AsyncDelegate(AddressOf UpdateProgress) 
    Private DataLoader As AsyncDelegate 
    Private DataLoaderIAsync As IAsyncResult 
    Private UpdateIncrement As Integer 

    Private Sub LoadListAttendance() 
     Dim txtLine As String = "" 
     ListView1.Items.Clear() 
     If File.Exists(strFileNameAndPath) = True Then 
      Dim objReader As New StreamReader(strFileNameAndPath) 

      Do While objReader.Peek() <> -1 
       Dim strLine As String = objReader.ReadLine() 
       If Split(strLine, " ")(0) = "" Then MessageBox.Show("Invalid file.", SysMsg, MessageBoxButtons.OK) : Exit Sub 
       ListView1.Items.Add(Split(strLine, " ")(0)) 
       ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(Mid(Split(strLine, " ")(1), 1, 8)) 
       ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(Mid(Split(strLine, " ")(1), 9, 4)) 
       ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(Mid(Split(strLine, " ")(1), 13, 2)) 
      Loop 
     End If 
    End Sub 


    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click 
     'Initialize the progress bar. 

     'Run a scalar command to select the row count and set the progress maximum 

     'Calculate a percentage for the Row Changed event. 
     ' ie if total recs is 1,000,000 set the progress amount to 10% of that. 
     ' *See DT_RowChanged 

     UpdateIncrement = 100000 

     'DeEnable any controls you may not want the user to touch until the 
     'load is complete. 

     'Load the data Ascync 
     DataLoader = New AsyncDelegate(AddressOf LoadData) 
     DataLoaderIAsync = DataLoader.BeginInvoke(0, New AsyncCallback(AddressOf LoadData_Completed), Nothing) 


    End Sub 





    Private Sub LoadData() 

     'Your Load data code. 
     'This sample code loads 1,000,000 recs 

     'Do not access any controls in the procedure 
     If Not checkServer() = True Then frmServerSet.ShowDialog() 
     For x As Integer = 0 To ListView1.Items.Count - 1 
      'If RecExist_2("tbltxt", "date", ListView1.Items(x).SubItems(1).Text, "id", ListView1.Items(x).Text, False) = False Then 
      sqlSTR = "insert into tbltxt (id,date,time,code) values" 
      sqlSTR = sqlSTR & " ('" & ListView1.Items(x).Text & "','" & ListView1.Items(x).SubItems(1).Text & "','" & ListView1.Items(x).SubItems(2).Text & "', '" & ListView1.Items(x).SubItems(3).Text & "')" ','" & t1 & "')" 

      ExecuteSQLQuery(sqlSTR) 

      'End If 
     Next 
     MsgBox("ok") 
    End Sub 

    Private Sub LoadData_Completed() 

     'Async op has completed. 
     'Call the endinvoke to the delegate and unlock the UI 

     'Do not access any controls in this procedure 

     DataLoader.EndInvoke(DataLoaderIAsync) 

     Me.Invoke(New AsyncDelegate(AddressOf UnlockUI), New Object() {Nothing}) 

    End Sub 

    Private Sub UnlockUI() 
     'Do what you want to the UI Here. 
     ProgressBar1.Visible = False 

    End Sub 

    Private Sub UpdateProgress(ByVal value As Integer) 
     'Updates the progress bar from the thread it was 
     'created on. 
     If ProgressBar1.InvokeRequired Then 
      ProgressBar1.Invoke(ProgressUpdater, New Object() {value}) 
     Else 
      If value > ProgressBar1.Maximum Then value = ProgressBar1.Maximum 
      ProgressBar1.Value = value 
     End If 

    End Sub 

    Private Sub DT_RowChanged(ByVal sender As Object, ByVal e As System.Data.DataRowChangeEventArgs) Handles DT.RowChanged 
     'Handle a row change 
     'Do not access any controls from this procedure 

     Static counter As Integer = 1 

     'first make sure the row is being added 
     If e.Action = DataRowAction.Add Then 
      'next check for an increment. In this case I have a hard coded 
      '100,000 Which is 10% of the million records that I know I have. 

      'Do not update the progress bar on every record 

      If counter Mod UpdateIncrement = 0 Then 
       UpdateProgress(counter) 
      End If 
      counter += 1 
     End If 

    End Sub 
End Class 

답변

2

LoadData는 백그라운드 스레드에서 실행됩니다. 당신 수 없습니다이 스레드에서 목록보기 (심지어 값을 읽을) 이야기. 더 일반적으로. UI 스레드에서 UI에서 일부 모델을 빌드하면 (각 값을 특성으로 나타내는 사용자 정의 클래스 목록이 있음) 작업자 스레드에서 해당 모델을 사용할 수있게됩니다. 그런 다음 백그라운드 스레드에서 UI과 관련이없는 모델에만 액세스합니다.

또한 SQL을 만들기 위해 값을 연결하지 마십시오. 그것은 자살 행위입니다. 이것은 명백한 SQL 주입 위험입니다. 대신 매개 변수없는 쿼리 또는 매개 변수화를 처리하는 도구를 사용하십시오.

+0

선생님 내가 UI 스레드를 사용하여 어떤 생각도 없어 .. 내 샘플 코드에서 .. 내가 어디에 UI 스레드 일을 구현할지 모르겠다 .. 답장을 주셔서 감사합니다 .. –

+0

@DeorwinBensurto UI 스레드가 하나입니다 버튼 클릭 이벤트 핸들러와 같은 기능을 수행합니다. 그래서 Button2_Click은 UI 스레드에서 발생합니다. UI 스레드는 컨트롤과 대화 할 수 있습니다. 그러나 LoadData는 작업자 스레드에 있으며 ** 컨트롤을 말할 수 없습니다. –

관련 문제