| | |
Threading, Delegates and the Invoke method.
Please support our VB.NET advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2007
Posts: 17
Reputation:
Solved Threads: 0
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:
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.
Here's the code:
- Public Class Form1
- Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
- Dim nt As New newThread(Me)
- Dim t As New Threading.Thread(AddressOf nt.ThreadCode)
- t.Start()
- End Sub
- Public Sub CreateMessageBox()
- Dim f As New Form()
- f.Show()
- End Sub
- End Class
- Public Class newThread
- Public Delegate Sub Inform()
- Public sendInform As Inform
- Public main As Form
- Public Sub New(ByRef main As Form1)
- Me.main = main
- sendInform = New Inform(AddressOf main.CreateMessageBox)
- End Sub
- Public Sub ThreadCode()
- Threading.Thread.Sleep(2000)
- '*********************************************
- sendInform() (VS) main.Invoke(sendInform)
- '***********************************
- End Sub
- End Class
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.
•
•
•
•
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.
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.
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.
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.
•
•
•
•
But why is sendInform() attached to the new thread, when clealy, it points to the main thread's method?
The truth does not change according to our ability to stomach it.
![]() |
Similar Threads
- Generic method parser / invoker (C#)
- Finding length (Java)
- finding the length in java (Computer Science)
- This Should be Easy for You Guys! (Linux Servers and Apache)
- Would someone be so kind as to view and verify... (Java)
- how to get the folder list of client side? (C#)
- Playing with RMI in Tiger (Java)
- I need help..urgently (VB.NET)
- buzz game (Java)
Other Threads in the VB.NET Forum
- Previous Thread: Creating Crystal report using MS Access query
- Next Thread: How to link a data row to datetimepicker
| Thread Tools | Search this Thread |
"crystal .net .net2005 30minutes 2008 access add advanced application array assignment basic binary box button buttons center click code combo connectionstring convert cpu data database databasesearch datagrid datagridview design designer dissertation dissertations dissertationthesis dosconsolevb.net editvb.net employees excel exists firewall folder image images isnumericfuntioncall listview login map math memory mobile module msaccess mssqlbackend mysql navigate net number opacity pan peertopeervideostreaming picturebox picturebox2 port print printpreview record regex reports" reuse right-to-left save savedialog search serial socket sorting sqldatbase sqlserver storedprocedure string temp textbox timer txttoxmlconverter upload useraccounts usercontol usercontrol vb vb.net vb.netcode vb.nettoolboxvisualbasic2008sidebar vbnet vista visual visualbasic visualbasic.net visualstudio.net web wpf xml





