Author Topic: SyncContext  (Read 1306 times)

0 Members and 1 Guest are viewing this topic.

clint78z

  • Mosquito
  • Posts: 5
SyncContext
« on: October 19, 2015, 03:32:00 PM »
Just wanted to say I have been reading this website for quite a while haven't posted much. I am one of the rare few who hasn't made the switch to C#. I have pused VB.net much further than most. The amount of tutorials start getting limited when you get more advanced with VB.net.

I was reading Keans webpage through the interface about using SyncContext for dealing with multithreading in AutoCad http://through-the-interface.typepad.com/through_the_interface/2013/01/integrating-leap-motion-and-autocad-basic-navigation.html. I think I have the concept down. Created a dummy form in Autocad on the main thread then initialialize it, set your synccontext to current. Spin up a thread, used the synccontext.post to send it back to the main thread.

Below I can make it work with a VB.net windows form class.

Code: [Select]
Imports System.IO
Imports System.Threading

Public Class Form1
    Private m_SyncContext As System.Threading.SynchronizationContext
    Private m_DestinationPath As String
    Private thread As Thread
    Public Sub New()
        ' This call is required by the designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.
        Me.m_SyncContext = System.Threading.SynchronizationContext.Current
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim id As Integer = thread.CurrentThread.ManagedThreadId
        thread = New Thread(AddressOf searchDwgs)
        thread.Start()
    End Sub

    Private Sub searchDwgs()
        ' searching through an opening drawings on the thread pool
        Me.m_SyncContext.Post(AddressOf UpdateTextBox, "[New Text]")
    End Sub

    Private Sub UpdateTextBox(param As Object)
        If (TypeOf (param) Is String) Then
            Me.TextBox3.AppendText(CStr(param))
        End If
    End Sub
End Class

Now I have trouble making it work if  I try an make it work just from the console, and put in a dummy form to get it going like Keans example.

Code: [Select]
Imports System.IO
Imports System.Threading
Imports System.Windows.Forms

Module Module1
    Dim m_frmDummy As Form
    Dim m_SyncContext As System.Threading.SynchronizationContext
    Dim id As Integer
    Dim thread As Thread
    Sub Main()
        m_frmDummy = New Form
        m_frmDummy.InitializeLifetimeService()
        m_SyncContext = System.Threading.SynchronizationContext.Current
        id = thread.CurrentThread.ManagedThreadId
        thread = New Thread(AddressOf searchDwgs)
        thread.Start()
    End Sub
    Private Sub searchDwgs()
        ' searching through an opening drawings on the thread pool
        m_SyncContext.Post(AddressOf UpdateTextBox, "[New Text]")
    End Sub

    Private Sub UpdateTextBox(param As Object)
        If (TypeOf (param) Is String) Then
            MsgBox(CStr(param))
        End If
    End Sub
End Module