Solved

generalized thread safe component of windows.form in .NET

Posted on 2012-03-26
4
361 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
ID: 37765127
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
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
        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
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?
0
 
LVL 14

Author Closing Comment

by:systan
ID: 37770146
you got the point
thanks
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

830 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