?
Solved

generalized thread safe component of windows.form in .NET

Posted on 2012-03-26
4
Medium Priority
?
377 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 2000 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

764 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