vb.net广义线程安全windows.form

时间:2012-03-25 08:15:36

标签: vb.net datagridview thread-safety

    ''//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”)

  

但是如果我有很多成员或属性可以使用,就像这段代码一样:

    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()

我应该怎样做才能使这个简单且通常是一个很好的线程安全组件

有什么想法吗?我可以这样称呼他们:

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

或者有更好的方法吗

1 个答案:

答案 0 :(得分: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)