同步上下文
只是想说我已经阅读这个网站很长一段时间没有发布太多。我是少数几个没有切换到C#的人之一。我比大多数人更进一步地 VB.net。当您更高级地使用 VB.net 时,教程的数量开始受到限制。我正在通过界面阅读Keans网页,了解如何使用SyncContext来处理AutoCad http://through-the-interface.typepad.com/through_the_interface/2013/01/integrating-leap-motion-and-autocad-basic-navigation.html 中的多线程处理。我想我已经把这个概念写下来了。在主线程上的Autocad中创建一个虚拟表单,然后初始化它,将synccontext设置为current。启动一个线程,使用 synccontext.post 将其发送回主线程。
下面我可以让它与 VB.net 窗口窗体类一起使用。
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, "")
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
现在,如果我尝试让它从控制台工作,并放入一个虚拟形式来让它像Keans的例子一样运行,我就很难让它工作。
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, "")
End Sub
Private Sub UpdateTextBox(param As Object)
If (TypeOf (param) Is String) Then
MsgBox(CStr(param))
End If
End Sub
End Module
**** Hidden Message *****
页:
[1]