vb.net обобщенный потокобезопасный windows.form - PullRequest
1 голос
/ 25 марта 2012
    ''//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 голосов
/ 21 мая 2012
''//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)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...