2012-03-25 68 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() 

我應該怎麼做,使這個簡單,一般一個不錯的線程SAF e組件

任何想法?我可以打電話給他們這樣的:

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

還是有這樣

+0

我已經解決了這個問題的一個更好的方式,感謝StackOverflow的 – XenKid 2012-03-27 07:57:55

+0

那麼你應該回答自己的問題,這樣的休息的世界可以受益。 – 2012-05-10 19:17:39

回答

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)