2012-03-25 12 views
1
''//begin cross threaded component 
Private Sub dBgRIDvIEWNotInvokeRequired(ByVal dBGridViewcomponentname As DataGridView, ByVal dvalue As String) 
    dBGridViewcomponentname.Text = dvalue 
    dBGridViewcomponentname.Update() 
End Sub 
Private Delegate Sub deldBGrridView(ByVal dBGridViewcomponentname As DataGridView, ByVal dvalue As String) 
Private Sub ThreadedDbGridViewAddress(ByVal dBGridViewcomponentname As DataGridView, ByVal dvalue As String) 
    Try 
     If InvokeRequired Then 
      Dim udd As New deldBGrridView(AddressOf ThreadedDbGridViewAddress) 
      Invoke(udd, New Object() {dBGridViewcomponentname, dvalue}) 
     Else 
      dBgRIDvIEWNotInvokeRequired(dBGridViewcomponentname, dvalue) 
     End If 
    Catch ex As Exception 
    End Try 
End Sub 
Private CrossThreadedDbGridView As New deldBGrridView(AddressOf ThreadedDbGridViewAddress) 
''//end cross threaded component 

このCrossThreadedDbGridView(DataGridView1、 "TheText")のようなものですvb.net一般スレッドセーフwindows.form

しかし、私はメンバーまたは財産の多くを持っている場合このコードのように使用するには:

DbGridPapers.ColumnCount = 5 
    DbGridPapers.RowCount = rc 
    DbGridPapers.Update() 
    DbGridPapers.Columns(0).HeaderText = "PaperSize" 
    DbGridPapers.Columns(1).HeaderText = "#of_Pages" 
    DbGridPapers.Columns(2).HeaderText = "#of_Images" 
    DbGridPapers.Columns(3).HeaderText = "Payable" 
    DbGridPapers.Columns(4).HeaderText = "CountedImage" 
    DbGridPapers.Update() 
    DbGridPapers.Rows(rc).HeaderCell.Value = FileName 
    DbGridPapers.Rows(rc).Cells(0).Value = xpapersize 
    DbGridPapers.Rows(rc).Cells(1).Value = xpNumbers 
    DbGridPapers.Rows(rc).Cells(2).Value = xiNumbers 
    DbGridPapers.Rows(rc).Cells(3).Value = xTotImageCounts 
    DbGridPapers.Rows(rc).Cells(4).Value = xTotImageCounts 
    DbGridPapers.Update() 

この単純な、一般的に素晴らしいスレッドセーフにするにはどうすればよいですかeコンポーネント

アイデア?私はこのようにそれらを呼び出すことができること:

CrossThreadedDbGridView(DataGridView1.ColumnCount, 5) 
    CrossThreadedDbGridView(DataGridView1.RowCount, rc) 
    CrossThreadedDbGridView(DataGridView1.Columns(0).HeaderText, "PaperSize") 
    ... 
    ... 

またはこの

+0

を行うための良い方法はあり休息ように私は問題を解決した、おかげで – XenKid

+0

をstackoverflowの後、あなたがあなた自身の質問に答える必要があります世界の恩恵を受けることができます。 –

答えて

0
''//It's not really a generalized safe thread datagridview, i just input the details to run safely. 
''//This is a bad programming code style I know, but there's many way to kill a chicken. 


    ''//begin cross threaded DataGridView component 
    Private Sub dBgRIDvIEWNotInvokeRequired(ByVal yselect As Integer, ByVal dBGridViewcomponentname As DataGridView, _ 
    ByVal dvalue As Integer, ByVal yfilename As String, ByVal ypapersize As String, _ 
    ByVal ynumpages As Integer, ByVal yimagecount As Integer, ByVal yregularprice As Decimal, ByVal ycountedimages As Integer) 
     If yselect = 1 Then 
      dBGridViewcomponentname.ColumnCount = 5 
      dBGridViewcomponentname.RowCount = dvalue 
      dBGridViewcomponentname.Update() 
      dBGridViewcomponentname.Columns(0).HeaderText = "PaperSize" 
      dBGridViewcomponentname.Columns(1).HeaderText = "Charge_Pages" 
      dBGridViewcomponentname.Columns(2).HeaderText = "#of_Images" 
      dBGridViewcomponentname.Columns(3).HeaderText = "Payable" 
      dBGridViewcomponentname.Columns(4).HeaderText = "CountedImage" 
      dBGridViewcomponentname.Update() 
     ElseIf yselect = 2 Then 
      dBGridViewcomponentname.Rows(dvalue).ErrorText = yfilename 
      dBGridViewcomponentname.Rows(dvalue).HeaderCell.Value = yfilename 
      dBGridViewcomponentname.Rows(dvalue).Cells(0).Value = ypapersize 
      dBGridViewcomponentname.Rows(dvalue).Cells(1).Value = ynumpages 
      dBGridViewcomponentname.Rows(dvalue).Cells(2).Value = yimagecount 
      dBGridViewcomponentname.Rows(dvalue).Cells(3).Value = yregularprice 
      dBGridViewcomponentname.Rows(dvalue).Cells(4).Value = ycountedimages 
      dBGridViewcomponentname.Update() 
     ElseIf yselect = 3 Then 
      dBGridViewcomponentname.Update() 
     ElseIf yselect = 4 Then 
      dBGridViewcomponentname.Rows.RemoveAt(dvalue) 
     End If 

    End Sub 
    Private Sub CrossThreadedDbGridView(ByVal yselect As Integer, ByVal dBGridViewcomponentname As DataGridView, _ 
    ByVal dvalue As Integer, ByVal yfilename As String, ByVal ypapersize As String, _ 
    ByVal ynumpages As Integer, ByVal yimagecount As Integer, ByVal yregularprice As Decimal, ByVal ycountedimages As Integer) 
     Try 
      If InvokeRequired Then 
       Dim udd As New deldBGrridView(AddressOf CrossThreadedDbGridView) 
       Invoke(udd, New Object() {yselect, dBGridViewcomponentname, dvalue, yfilename, ypapersize, ynumpages, yimagecount, yregularprice, ycountedimages}) 
      Else 
       dBgRIDvIEWNotInvokeRequired(yselect, dBGridViewcomponentname, dvalue, yfilename, ypapersize, ynumpages, yimagecount, yregularprice, ycountedimages) 
      End If 
     Catch ex As Exception 
     End Try 
    End Sub 
    Private Delegate Sub deldBGrridView(ByVal yselect As Integer, ByVal dBGridViewcomponentname As DataGridView, _ 
    ByVal dvalue As Integer, ByVal yfilename As String, ByVal ypapersize As String, _ 
    ByVal ynumpages As Integer, ByVal yimagecount As Integer, ByVal yregularprice As Decimal, ByVal ycountedimages As Integer) 
    ''//end cross threaded DataGridView component 


''//usage: 
CrossThreadedDbGridView(2, DbGridPapers, rc, FileName, xpapersize, xpNumbers, xiNumbers, ((xRegularPrice * xpNumbers) + xTotImageCounts), xTotImageCounts)