generalized thread safe component of windows.form in .NET

Posted on 2012-03-26
Last Modified: 2012-08-13
when used is like this CrossThreadedDbGridView(DataGridView1, "TheText")
   ''//begin cross threaded component
Private Sub dBgRIDvIEWNotInvokeRequired(ByVal dBGridViewcomponentname As DataGridView, ByVal dvalue As String)
    dBGridViewcomponentname.Text = dvalue
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)
        If InvokeRequired Then
            Dim udd As New deldBGrridView(AddressOf ThreadedDbGridViewAddress)
            Invoke(udd, New Object() {dBGridViewcomponentname, dvalue})
            dBgRIDvIEWNotInvokeRequired(dBGridViewcomponentname, dvalue)
        End If
    Catch ex As Exception
    End Try
End Sub
Private CrossThreadedDbGridView As New deldBGrridView(AddressOf ThreadedDbGridViewAddress)
''//end cross threaded component

Open in new window

But what if I have lots of member or property to used like this code:
DbGridPapers.ColumnCount = 5
    DbGridPapers.RowCount = rc
    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.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

Open in new window

What should I do to make this simple and generally a nice thread safe component
Any ideas? that I can call them like this:
CrossThreadedDbGridView(DataGridView1.ColumnCount, 5)
    CrossThreadedDbGridView(DataGridView1.RowCount, rc)
    CrossThreadedDbGridView(DataGridView1.Columns(0).HeaderText, "PaperSize")

Open in new window

Or is there a better way of doing this, please let me know
Question by:systan
  • 2
  • 2
LVL 83

Accepted Solution

CodeCruiser earned 500 total points
ID: 37765127
You can write a utility method which takes the grid name, property name and property value and uses reflection to set value
LVL 14

Author Comment

ID: 37765616
Ok, lets have another simple sample:
''//begin cross threaded TextBox component
    Private Sub TextBoxNotInvokeRequired(ByVal TextBoxcomponentname As TextBox, ByVal dvalue As String)
        TextBoxcomponentname.Text = dvalue
        ''//Dim info As PropertyInfo = TextBoxcomponentname.GetType().GetProperty("")

    End Sub
    Private Delegate Sub delTextBox(ByVal TextBoxcomponentname As TextBox, ByVal dvalue As String)
    Private Sub ThreadedTextBoxAddress(ByVal TextBoxcomponentname As TextBox, ByVal dvalue As String)
            If InvokeRequired Then
                Dim udd As New delTextBox(AddressOf ThreadedTextBoxAddress)
                Invoke(udd, New Object() {TextBoxcomponentname, dvalue})
                TextBoxNotInvokeRequired(TextBoxcomponentname, dvalue)
            End If
        Catch ex As Exception
        End Try
    End Sub
    Private CrossThreadedTextBox As New delTextBox(AddressOf ThreadedTextBoxAddress)
    ''//end cross threaded TextBox component

Open in new window

We can use that with:
CrossThreadedTextBox(Textbox1, "TheText")

But what about if I have this:

I can't write like this:
CrossThreadedTextBox(Textbox1.focus, "")

How is that related to PropertyInfo?
Can you write a short code for that, the focus and select properties

LVL 83

Expert Comment

ID: 37766354
That is not related to property info. None of your code seems to have anything related to focus/select etc.

The basic question, what are you trying to achieve overall?
LVL 14

Author Closing Comment

ID: 37770146
you got the point

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now