Solved

generalized thread safe component of windows.form in .NET

Posted on 2012-03-26
4
348 Views
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
    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

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

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
0
Comment
Question by:systan
  • 2
  • 2
4 Comments
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
Comment Utility
You can write a utility method which takes the grid name, property name and property value and uses reflection to set value

http://stackoverflow.com/questions/1089123/setting-a-property-by-reflection-with-a-string-value

http://stackoverflow.com/questions/619767/net-reflection-set-object-property
0
 
LVL 14

Author Comment

by:systan
Comment Utility
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
        TextBoxcomponentname.Update()
        ''//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)
        Try
            If InvokeRequired Then
                Dim udd As New delTextBox(AddressOf ThreadedTextBoxAddress)
                Invoke(udd, New Object() {TextBoxcomponentname, dvalue})
            Else
                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:
Textbox1.focus()
Textbox1.Select()

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

thanks
0
 
LVL 83

Expert Comment

by:CodeCruiser
Comment Utility
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?
0
 
LVL 14

Author Closing Comment

by:systan
Comment Utility
you got the point
thanks
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

772 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

11 Experts available now in Live!

Get 1:1 Help Now