5 Replies - 2845 Views - Last Post: 08 June 2011 - 01:55 PM Rate Topic: -----

#1 musya  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 11
  • View blog
  • Posts: 1,012
  • Joined: 25-April 07

Threading and delegates

Posted 08 June 2011 - 11:31 AM

My delegate:
Delegate Sub taskTrayCallback()


        If Me.DelegationsTreeView.InvokeRequired Then
            MessageBox.Show("INVOKE")
            Dim d As New taskTrayCallback(AddressOf taskTraySetup)
            Me.Invoke(d, New Object())
        Else
            MessageBox.Show("no invoke!!!")
        End If


So this is my code that runs on a seperate thread. What happens is the code runs, shows me a message box that says "INVOKE", then thats it. It doesnt run the sub again, doesnt do what its suppose to. I'm wondering if anyone sees whats wrong with this code?

Is This A Good Question/Topic? 0
  • +

Replies To: Threading and delegates

#2 Psyguy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 71
  • View blog
  • Posts: 320
  • Joined: 12-January 11

Re: Threading and delegates

Posted 08 June 2011 - 12:05 PM

I am assuming your sub looks like this?

Private Sub taskTraySetup()

If Me.DelegationsTreeView.InvokeRequired Then
     MessageBox.Show("INVOKE")
     Dim d as New taskTrayCallBack(AddressOf taskTraySetup)
     Me.Invoke(d, New Object())
Else
     MessageBox.Show("no invoke!!!")
End If

End Sub



Is that correct?

Edit: this is exactly how I do it and mine work.

This post has been edited by Psyguy: 08 June 2011 - 12:07 PM

Was This Post Helpful? 0
  • +
  • -

#3 musya  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 11
  • View blog
  • Posts: 1,012
  • Joined: 25-April 07

Re: Threading and delegates

Posted 08 June 2011 - 12:26 PM

Yes thats how it looks only my sub is public
Was This Post Helpful? 0
  • +
  • -

#4 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 991
  • View blog
  • Posts: 971
  • Joined: 30-September 10

Re: Threading and delegates

Posted 08 June 2011 - 01:16 PM

Hi,

Change this line:

Me.Invoke(d, New Object())


to:

Me.DelegationsTreeView.Invoke(d)



The thing that should make the difference to your program is the change in the arguments you pass to Invoke().

The second argument that Invoke() can take specifies the arguments you want to pass to the method encapsulated by the delegate your invoking. Since taskTraySetup() doesn't take any arguments, you don't need to pass a second argument to Invoke(). You were passing a new Object to taskTraySetup() :)


I also changed the code so Invoke() is called on the DelegationTreeView (since that is what you check the InvokeRequired property on). It shouldn't matter either way though :)

This post has been edited by CodingSup3rnatur@l-360: 08 June 2011 - 01:29 PM

Was This Post Helpful? 0
  • +
  • -

#5 musya  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 11
  • View blog
  • Posts: 1,012
  • Joined: 25-April 07

Re: Threading and delegates

Posted 08 June 2011 - 01:48 PM

Dang still nothing, the sub never runs again...
Was This Post Helpful? 0
  • +
  • -

#6 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 991
  • View blog
  • Posts: 971
  • Joined: 30-September 10

Re: Threading and delegates

Posted 08 June 2011 - 01:55 PM

This is the all the code I have in a Form class I just created (I run the sub from a button click handler):

Public Class Form1

    Private Delegate Sub taskTrayCallback()

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       
         Dim t1 As New Threading.Thread(New Threading.ThreadStart(AddressOf   Me.taskTraySetup))
         t1.Start()
    End Sub

    Private Sub taskTraySetup()

        If Me.DelegationsTreeView.InvokeRequired Then
            MessageBox.Show("INVOKE")
            Dim d As New taskTrayCallback(AddressOf taskTraySetup)
            Me.DelegationsTreeView.Invoke(d)
        Else
            MessageBox.Show("no invoke!!!")
        End If

    End Sub

End Class



And that does work. When I click the button, the sub is called in a new thread, with "INVOKE" being displayed. The sub is then called again when Invoke() is called, with "no invoke!!!" being displayed.

Perhaps compare what you have to the code above? :)

This post has been edited by CodingSup3rnatur@l-360: 08 June 2011 - 01:56 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1