Threading, Delegates and the Invoke method.

Please support our VB.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2007
Posts: 17
Reputation: Sebouh is an unknown quantity at this point 
Solved Threads: 0
Sebouh Sebouh is offline Offline
Newbie Poster

Threading, Delegates and the Invoke method.

 
0
  #1
Sep 3rd, 2007
Hi guys. I was messing with Threading and stuff, and i have reached a point where i'm not sure what's causing the current behavior.

Here's the code:
  1. Public Class Form1
  2. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  3. Dim nt As New newThread(Me)
  4. Dim t As New Threading.Thread(AddressOf nt.ThreadCode)
  5. t.Start()
  6. End Sub
  7. Public Sub CreateMessageBox()
  8. Dim f As New Form()
  9. f.Show()
  10. End Sub
  11. End Class
  12. Public Class newThread
  13. Public Delegate Sub Inform()
  14. Public sendInform As Inform
  15. Public main As Form
  16. Public Sub New(ByRef main As Form1)
  17. Me.main = main
  18. sendInform = New Inform(AddressOf main.CreateMessageBox)
  19. End Sub
  20. Public Sub ThreadCode()
  21. Threading.Thread.Sleep(2000)
  22. '*********************************************
  23. sendInform() (VS) main.Invoke(sendInform)
  24. '***********************************
  25. End Sub
  26. End Class
The problem is in the last statement, when i use main.Invoke() the CreatemessageBox creates a msgbx, which is part of the Form1. But when i use sendInform() I get a messagebx that appears, but is part of the new thread. So if it were a Form instead of a msgbx, it would appear and then close as soon as the new thread exits.

My questionis , obviously, why? Why is one working the way i want it to and the other not.

My theory is that, when the main.invoke() is used, the loader(?) already assigns the address of the method as the main thread's Createmsgbx. But so when i use invoke(), it is somehow assigning the new thread's createmsgbx address. But i have a feeling i'm wrong, cause i know that threads share code, so the addresses must be the same. So why i one getting "assigned" as a call from the main thread, and the other as a method call from the new thread?

Thanks a lot.
Last edited by Sebouh; Sep 3rd, 2007 at 5:10 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 322
Reputation: Hamrick will become famous soon enough Hamrick will become famous soon enough 
Solved Threads: 33
Hamrick's Avatar
Hamrick Hamrick is offline Offline
Posting Whiz

Re: Threading, Delegates and the Invoke method.

 
0
  #2
Sep 3rd, 2007
The problem is in the last statement, when i use main.Invoke() the CreatemessageBox creates a msgbx, which is part of the Form1. But when i use sendInform() I get a messagebx that appears, but is part of the new thread. So if it were a Form instead of a msgbx, it would appear and then close as soon as the new thread exits.
Invoke runs the delegate from the new thread but it's attached to the thread of the control that calls invoke. When you do main.Invoke( sendInform ) sendInform runs synchronously but is still attached to main's thread and has the same lifetime. When you do sendInform() sendInform runs synchronously but is attached to the new thread and has the same lifetime as the new thread and that's why it closes when the thread ends.
The truth does not change according to our ability to stomach it.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 17
Reputation: Sebouh is an unknown quantity at this point 
Solved Threads: 0
Sebouh Sebouh is offline Offline
Newbie Poster

Re: Threading, Delegates and the Invoke method.

 
0
  #3
Sep 4th, 2007
But why is sendInform() attached to the new thread, when clealy, it points to the main thread's method? I mean, how did the compiler decide that?
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 322
Reputation: Hamrick will become famous soon enough Hamrick will become famous soon enough 
Solved Threads: 33
Hamrick's Avatar
Hamrick Hamrick is offline Offline
Posting Whiz

Re: Threading, Delegates and the Invoke method.

 
0
  #4
Sep 4th, 2007
Methods aren't owned by any one thread, they're just a bunch of instructions without any data. When you call a method, you have to say what thread the call runs against. Unless you do something special like use Invoke, the call is attached to the thread that makes the call.

But why is sendInform() attached to the new thread, when clealy, it points to the main thread's method?
It doesn't point to the main thread's because you call it in the new thread. With Invoke it points to the main thread because the new thread is calling it on behalf of the main thread. Without Invoke to say that you want the method to run against the owning window handle, the current thread has to assume that it owns the call.
The truth does not change according to our ability to stomach it.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 17
Reputation: Sebouh is an unknown quantity at this point 
Solved Threads: 0
Sebouh Sebouh is offline Offline
Newbie Poster

Re: Threading, Delegates and the Invoke method.

 
0
  #5
Sep 5th, 2007
I understand. But when the main thread is closed, does the new thread get closed too (because it's a child)? I tryed it and didn't get an error message about not being able to call callFinished.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 322
Reputation: Hamrick will become famous soon enough Hamrick will become famous soon enough 
Solved Threads: 33
Hamrick's Avatar
Hamrick Hamrick is offline Offline
Posting Whiz

Re: Threading, Delegates and the Invoke method.

 
0
  #6
Sep 5th, 2007
Threads run until they're finished or you stop them by force.
The truth does not change according to our ability to stomach it.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC