Cross-thread VB.NET 2008 Communication WITHOUT forms

mediasoftware
mediasoftware used Ask the Experts™
on
Hello, I am developing a VB.NET 2008 application.

Although this is not a console application, this 'Windows Forms Application' consists of only a module (no UI, no Forms, no Controls, no Classes, ONLY a module).

Now, in that module, I call an asynchronous TcpClient BeginRead, passing 'AdressOf DoRead' as CallBack.

When the DoRead sub is executed, it runs on a different thread than the main module, and any call from here on executes on this thread.

What I need is a way to call a sub in the main thread from the CallBack thread (sub DoRead), all this without any UI components or forms.

I want a solution strictly to my problem, not a workaround involving Forms or synchronous read calls.

Thank you.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
If there are no UI components then why do you need to switch threads?  Just call your desired sub directly from the callback...

Author

Commented:
Thank for your comment, and as a message for further experts, please do not ask questions like: "Why do you need this ?" I need a solution for my problem.

To answer your question, if I call directly from the callback, the sub will run on the callback thread, NOT the main thread. Please read my request carefully.

Thank you in advance.
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009
Commented:
You can use a WindowsFormsSynchronizationContext:

Main: 9
Main: 9
Main: 9
Threaded: 10
Main: 9
SynchronizedCallBack: 9
Main: 9
Main: 9
Main: 9
Main: 9
Threaded: 10
Main: 9
SynchronizedCallBack: 9
Main: 9
Main: 9
Main: 9

Produced with:
Imports System.Threading
Module Module1

    Private SC As New WindowsFormsSynchronizationContext

    Public Sub Main()
        Dim T As New System.Threading.Thread(AddressOf Threaded)
        T.IsBackground = True
        T.Start()

        While True
            Thread.Sleep(1000)
            Debug.Print("Main: " & Thread.CurrentThread.ManagedThreadId)
            Application.DoEvents() ' <-- otherwise the "Posted" SynchronizedCallBack() calls won't get executed
        End While
    End Sub

    Private Sub SynchronizedCallBack()
        Debug.Print("SynchronizedCallBack: " & Thread.CurrentThread.ManagedThreadId)
    End Sub

    Private Sub Threaded()
        While True
            Thread.Sleep(5000)
            Debug.Print("Threaded: " & Thread.CurrentThread.ManagedThreadId)
            SC.Post(New SendOrPostCallback(AddressOf SynchronizedCallBack), Nothing)
        End While
    End Sub

End Module

Open in new window

Author

Commented:
Thank you very much, I didn't know of the WindowsFormsSynchronizationContext.

This works great and was very easy to implement all over my application.

I also have another posted question, if you would like to take a look:
http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Studio_.NET_2005/Q_26303641.html?cid=1575#a33126401

Thank you very much.
Looking forward to working with you again.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial