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:

[LIST=1]
[*]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[/LIST]

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.

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.

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?

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.

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.

Threads run until they're finished or you stop them by force.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.