3 Replies - 560 Views - Last Post: 04 September 2012 - 07:08 AM Rate Topic: -----

#1 Criel  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 128
  • Joined: 11-March 11

CrossThreadExcpetion Delegate help

Posted 30 August 2012 - 10:53 AM

Hi,

I am currently trying to integrate threading into one method of my application and am getting this error when it's trying to set the text of the component. This is because you cannot change anything to the UI from another thread other than the original so I must use a delegate to have the method finish on the original thread before going on.(I think, if i understand this correctly)

My issue I'm having is that when I try to implement my delegate I'm getting an error

method = property evaluation failed


this also freezes my debugger and forces me to close the application. Am I declaring the delegate incorrectly? The signature for the method is the same.

Public Delegate Sub testControlToDisplay(ByVal sControlName As String, ByVal sUIName As String, ByRef IsEnabled As Boolean, ByRef IsActive As Boolean, ByVal frmctrl As String) 'Declared globally

Dim delTest As testControlToDisplay 'Declared in display method
delTest = New testControlToDisplay(AddressOf getControlToDisplay) 'Declared in display method

 cmdPriName.Invoke(delTest, "cmdPriName", sUIName, bEnable, bContinue, "fraName")'Called in the display method


The function(getControlToDisplay) gets called around 50 times for each component on the form to determine if a certain component should be visible or not depending on the type of account. Is a delegate used for multiple calls or is it a one time only type of thing?


Edit: I have found a way that works well but it's not calling the function as it should, it will get to the line and skip over it immediately without going to the function.

cmdPriName.Invoke(New testControlToDisplay(AddressOf getControlToDisplay), "cmdPriName", sUIName, bEnable, bContinue, "fraName")

This post has been edited by Criel: 30 August 2012 - 11:30 AM


Is This A Good Question/Topic? 0
  • +

Replies To: CrossThreadExcpetion Delegate help

#2 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 765
  • Joined: 19-October 11

Re: CrossThreadExcpetion Delegate help

Posted 30 August 2012 - 11:47 AM

Your Delegate has no Control as a parameter, so you are unable (or have much harder job) to find out, if that particular control requires invocation.

I suggest you change Delegate signature to:
Public Delegate Sub testControlToDisplay(ByVal myControl as Control, ByVal sControlName As String, ByVal sUIName As String, ByRef IsEnabled As Boolean, ByRef IsActive As Boolean, ByVal frmctrl As String) 

but use specific control instead of Control (like RichTextBox for example) and use it also in subroutine than.

Than in your code, you call invoke with 6 parameters, but your delegate only has 5. With the change I suggested, everything might just work fine, but you should also check, if invoke is required, before invoking.

You should also consider, if you need all those parameters, when you pass control to a method.

Your final code might look like this:
    ' The Delegate
    Public Delegate Sub testControlToDisplay(ByVal myControl as Control, ByVal sControlName As String, ByVal sUIName As String, ByRef IsEnabled As Boolean, ByRef IsActive As Boolean, ByVal frmctrl As String)

    ' Delegates Sub Method
    Public Sub getControlToDisplay(ByVal myControl as Control, ByVal sControlName As String, ByVal sUIName As String, ByRef IsEnabled As Boolean, ByRef IsActive As Boolean, ByVal frmctrl As String)
        ' If InvokeRequired returns TRUE (comparing calling and creator thread ID's) then...
        If myControl.InvokeRequired Then
            Dim delTest As New testControlToDisplay(AddressOf getControlToDisplay)
            'also I suggest you use Me (me = this class instance (like Form1 for example))
            Me.Invoke(delTest, New Object() {myControl, sControlName, sUIName, IsEnabled, IsActive, frmctrl})
        Else
            myControl.Name = sControlName 'for example
            '... and so on ...
        End If
    End Sub



Than you call this sub like this:
'myControl should be some specific control, like RichTextBox, and the same as sub and delegate is
getControlToDisplay(myControl, "My Name", "UI Name", TRUE, TRUE, "My frm ctrl")



Edit: fixed some mistakes in a hurry

This post has been edited by lucky3: 30 August 2012 - 11:56 AM

Was This Post Helpful? 1
  • +
  • -

#3 Criel  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 128
  • Joined: 11-March 11

Re: CrossThreadExcpetion Delegate help

Posted 04 September 2012 - 07:01 AM

Thanks for the help! It's really cool to watch this delegate go to work through the debugger. Thanks again
Was This Post Helpful? 0
  • +
  • -

#4 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 765
  • Joined: 19-October 11

Re: CrossThreadExcpetion Delegate help

Posted 04 September 2012 - 07:08 AM

You're wellcome :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1