Results 1 to 21 of 21

Thread: [RESOLVED] Threading, Invoking, Delegating and crazyness?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2001
    Posts
    364

    Resolved [RESOLVED] Threading, Invoking, Delegating and crazyness?

    Hello,
    right, i think i need to use invoking and delegating and stuff, but i really cant get my head around it, i have searched and seen other posts and tutorials, but cant figure out how to do it in my project.

    ok, i have class, which has a thread, and in this class in part of the code i want to get the webbrowser on form1 to refresh.

    so would I put all the code in the class inside
    Private Delegate Sub dont know what goes here

    and something like:
    form1.invoke (webbrowser1.refresh()) ??


    hmm im confused

    can anyone help?

    thanks very much
    Richard <><

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Threading, Invoking, Delegating and crazyness?

    You would create a method in your form that does what you want and then a delegate with the same signature. You then call Invoke on your form from another thread and pass a new instance of your delegate:
    VB Code:
    1. Private Delegate Sub DoSomethingDelegate()
    2.  
    3.     Private Sub DoSomething()
    4.         'Do something here.
    5.     End Sub
    6.  
    7.     Private Sub CallDoSomethingViaDelegate()
    8.         Me.Invoke(New DoSomethingDelegate(AddressOf DoSomething))
    9.     End Sub
    If the method you want to invoke has no arguments or return value then you don't need to declare your own delegate:
    VB Code:
    1. Private Sub DoSomething()
    2.         'Do something here.
    3.     End Sub
    4.  
    5.     Private Sub CallDoSomethingViaMethodInvoker()
    6.         Me.Invoke(New MethodInvoker(AddressOf DoSomething))
    7.     End Sub
    If the method you want to invoke does have arguments or a return value then you must declare your own delegate with the same signature:
    VB Code:
    1. Private Delegate Function DoSomethingDelegate(ByVal arg As String) As Integer
    2.  
    3.     Private Function DoSomething(ByVal arg As String) As Integer
    4.         'Do something here.
    5.     End Function
    6.  
    7.     Private Sub CallDoSomethingViaDelegate()
    8.         Dim result As Integer = CInt(Me.Invoke(New DoSomethingDelegate(AddressOf DoSomething), New Object() {"string argument"}))
    9.     End Sub
    In your case, that means you need to define a procedure that calls WebBrowser1.Refresh() and then invoke it via a MethodInvoker instance, which is the easiest of all.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2001
    Posts
    364

    Re: Threading, Invoking, Delegating and crazyness?

    i tried the middle bit of code, but i says that invoke is not a member of windowsapplication1.clsserver, do i need to delcare something or something?

    and in the dosomething() sub is the code form1.webbrowser1.navigate("address") correct?
    Richard <><

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Threading, Invoking, Delegating and crazyness?

    Invoke is a member of the Control class, so you need to call it on a control of some sort, like the form. I was assuming that the code would be called from the same form, which is why I used "Me", but it can be called from anywhere that you can see a reference to that from. The DoSomething method, or its equivalent in your code, would normally be in the same form as the WebBrowser control, so Me.WebBrowser1.Navigate would be appropriate.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2001
    Posts
    364

    Re: Threading, Invoking, Delegating and crazyness?

    ok, i have moved to code to the form and call the CallDoSomethingViaMethodInvoker from my class, but now i have this error:
    Invoke or BeginInvoke cannot be called on a control until the window handle has been created.
    Richard <><

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Threading, Invoking, Delegating and crazyness?

    Can you post the code that starts the worker thread and the code that calls Invoke? Please show which class each section is in. Also, is your form visible at the time you call Invoke?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2001
    Posts
    364

    Re: Threading, Invoking, Delegating and crazyness?

    yes form is visible.
    in form1:
    VB Code:
    1. Private Sub DoRefresh()
    2.         WebBrowser1.Navigate("http://www.delirious.co.uk")
    3.     End Sub
    4.  
    5.     Public Sub CallDoRefreshViaMethodInvoker()
    6.         Me.Invoke(New MethodInvoker(AddressOf DoRefresh))
    7.     End Sub

    in clsServer class:
    VB Code:
    1. Private Sub AcceptClient()
    2.         Dim pendingClient As TcpClient
    3.         Dim nameOfClient As String
    4.         Dim IPofClient As String
    5.         Dim refreshbrow As String
    6.         pendingClient = Listener.AcceptTcpClient()
    7.         reader = New System.IO.StreamReader(pendingClient.GetStream)
    8.         nameOfClient = reader.ReadLine()
    9.         IPofClient = reader.ReadLine()
    10.         refreshbrow = reader.ReadLine()
    11.  
    12.         Clients.Add(pendingClient, nameOfClient)
    13.         My.Computer.FileSystem.WriteAllText("C:\displog.txt", _
    14.         Date.Now + " ID:" + nameOfClient + " IP:" + IPofClient + " - Connected" + vbCrLf, True)
    15.         Clients.Clear()
    16.  
    17.         Form1.CallDoRefreshViaMethodInvoker()
    18.  
    19.     End Sub

    thanks
    Richard <><

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Threading, Invoking, Delegating and crazyness?

    The whole idea is that you cannot access a member of a control other than Invoke and the like across thread boundaries, and you are doing just that by calling CallDoRefreshViaMethodInvoker. You have to call the Invoke method from the second thread, so if the AcceptClient procedure is being executed in the second thread then you need call Invoke from there on an instance of the form.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2001
    Posts
    364

    Re: Threading, Invoking, Delegating and crazyness?

    oh, right ok, its very confusing this, i think im being a bit thick, but how do i do an instance of the form?
    Richard <><

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Threading, Invoking, Delegating and crazyness?

    The class that AcceptClient belongs to needs a reference to the form. How you get it one is up to you. Does this object get created by the form? If so, you can pass a reference to "Me" in the constructor. If not then you'll need to get a bit more creative. You may need to use a global variable but that should only be used if there is no other way to achieve it elegantly.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11
    Fanatic Member r0ach's Avatar
    Join Date
    Dec 1999
    Location
    South Africa
    Posts
    722

    Re: Threading, Invoking, Delegating and crazyness?

    Sorry, I haven't read too much of the post, but I have a question regarding threading, too.

    I have my form that starts a class in a new thread. This new class starts, and keeps track of several other classes in their own threads. This class needs to notify it's "child-classes" that it's about to kill the their threads, so that they can perform some cleanup. How can I notify these classes? I had a property on the class that gets set to true by the calling thread, but it doesn't seem like they're receiving this. Any advice?

    r0ach™
    Don't forget to rate the post

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Threading, Invoking, Delegating and crazyness?

    Quote Originally Posted by r0ach
    Sorry, I haven't read too much of the post, but I have a question regarding threading, too.

    I have my form that starts a class in a new thread. This new class starts, and keeps track of several other classes in their own threads. This class needs to notify it's "child-classes" that it's about to kill the their threads, so that they can perform some cleanup. How can I notify these classes? I had a property on the class that gets set to true by the calling thread, but it doesn't seem like they're receiving this. Any advice?
    This question is unrelated to the original question and should have been posted in a new thread.

    If you want to terminate a thread from the outside you need to call Abort on it and then within the thread you handle the ThreadAbortException that this raises and do your cleanup then.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  13. #13
    Fanatic Member r0ach's Avatar
    Join Date
    Dec 1999
    Location
    South Africa
    Posts
    722

    Re: Threading, Invoking, Delegating and crazyness?

    Sorry, jmcilhinney. Just thought that since we are on threads, I'd pop it in there. Won't happen again. Thanks for the help, though.

    r0ach™
    Don't forget to rate the post

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2001
    Posts
    364

    Re: Threading, Invoking, Delegating and crazyness?

    im still stuck on this whole referencing form1, i have tried:
    VB Code:
    1. Form1.Invoke(New MethodInvoker(AddressOf Form1.DoRefresh))

    thats the only way i know how to reference, i think, im still kinda a beginner.

    sorry for being stupid
    Richard <><

  15. #15
    Fanatic Member r0ach's Avatar
    Join Date
    Dec 1999
    Location
    South Africa
    Posts
    722

    Re: Threading, Invoking, Delegating and crazyness?

    No, not quite like that.

    Let's say you are starting a class in a new thread. You'd have the class's New() method looking like this:

    VB Code:
    1. Private m_frm As Form1
    2. Public Sub New(ByVal frm As Form1)
    3.   m_frm = frm
    4. Next

    Now, in the form, where you'd be starting the threads, you do this:
    VB Code:
    1. Dim MyObject As MyClass '// MyClass has the constructor above
    2. MyObject = New MyClass(Me) '// instantiate the class, passing this form (Form1)
    3. '// prepare thread
    4. Me.Invoke(New MethodInvoker(AddressOf MyObject.ThreadProcedure))
    5. '// ThreadProcedure is the Method in the class (MyClass) that will be running in it's own thread.

    r0ach™
    Don't forget to rate the post

  16. #16
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Threading, Invoking, Delegating and crazyness?

    Your class needs a variable that refers to the form. You can't just use the name of the class, if that is what that code does. That's like having your name assigned to a String variable and trying to access by calling a member of the String class rather than the variable itself. Does the form create this clsServer object? If so, simpy rewrite the constructor to accept an argument of type Form1, which it can then save to a Private variable. The form would then pass "Me" as this argument and voila, the object now has a reference to the form that it can use to Invoke methods.

    Edit:
    Yeah, like that.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  17. #17

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2001
    Posts
    364

    Re: Threading, Invoking, Delegating and crazyness?

    i have attached my project, its gone a bit crazy now, couldnt get the code to work, just erroring, have tried putting it in other places.
    Richard <><

  18. #18
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Threading, Invoking, Delegating and crazyness?

    I don't download projects unless I have a VERY compelling reason.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  19. #19
    Fanatic Member r0ach's Avatar
    Join Date
    Dec 1999
    Location
    South Africa
    Posts
    722

    Re: Threading, Invoking, Delegating and crazyness?

    Your first mistake was that you copied my sample JUST LIKE THAT into your project. Remove all my stuff from the form's code

    At the top of your form change it like this:
    VB Code:
    1. Dim server As New clsServer(Me)

    Now, in clsServer, you can access the webbrowser using the m_frm reference to Form1.

    r0ach™
    Don't forget to rate the post

  20. #20

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2001
    Posts
    364

    Re: Threading, Invoking, Delegating and crazyness?

    right thanks roach, that made it more understandable now, and after a bit of fidling it works, thanks very much, thanks jm for explaing threading and invoking better then msdn explained it, sorry if i seemed a bit stupid.
    thanks
    Richard <><

  21. #21
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] Threading, Invoking, Delegating and crazyness?

    Multi-threading is a complex topic and always takes some time to get your head around, even for the experts.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width