ActiveX 控件 '8856f961-340a-11d0-a96b-00c04fd705a2'无法实例化,因为当前线程不在单线程单元中

发布于 2024-12-25 15:27:10 字数 2266 浏览 0 评论 0原文

当我尝试显示其中包含网络浏览器的表单时,出现此错误。

ActiveX 控件“8856f961-340a-11d0-a96b-00c04fd705a2”无法实例化,因为当前线程不在单线程单元中

我通过以下方式调用它:

Public Class frmMain
  Class Server
       Public Shared Sub Main()
        Dim aTcpMessaging As IMessagingSystemFactory = New TcpMessagingSystemFactory()
        Dim anInputChannel As IInputChannel = aTcpMessaging.CreateInputChannel(theIPforLocal & ":" & thePort)
        Dim aStringMessagesFactory As IStringMessagesFactory = New StringMessagesFactory()
        Dim aStringMessageReceiver As IStringMessageReceiver = aStringMessagesFactory.CreateStringMessageReceiver()
        AddHandler aStringMessageReceiver.MessageReceived, AddressOf StringMessageReceived

        aStringMessageReceiver.AttachInputChannel(anInputChannel)
    End Sub

       Private Shared Sub StringMessageReceived()
            Call New frmMM().Show()
       End Sub
  End Class

  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim RelState As Integer = 0
    Call frmMain.Server.Main()
    lblVer.Text = "V.7"
    Pid = 0
  End Sub
End Class

如何加载它,这样它就不会不告诉我那个错误吗?

谢谢。

David

代码更新

 Class Server
    <STAThread()> Public Sub Main()
        Dim aTcpMessaging As IMessagingSystemFactory = New TcpMessagingSystemFactory()
        Dim anInputChannel As IInputChannel = aTcpMessaging.CreateInputChannel(theIPforLocal & ":" & thePort)
        Dim aStringMessagesFactory As IStringMessagesFactory = New StringMessagesFactory()
        Dim aStringMessageReceiver As IStringMessageReceiver = aStringMessagesFactory.CreateStringMessageReceiver()
        AddHandler aStringMessageReceiver.MessageReceived, AddressOf StringMessageReceived

        aStringMessageReceiver.AttachInputChannel(anInputChannel)
    End Sub

   Private Shared Sub StringMessageReceived()
        Call New frmMM().Show()
   End Sub
 End Class

  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim RelState As Integer = 0
    Call frmMain.Server.Main()   '<-- the error now
    lblVer.Text = "V.7"
    Pid = 0
  End Sub
End Class

I am getting this error when i try to show a form with an webbrowser in it.

ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the current thread is not in a single-threaded apartment

I am calling it by:

Public Class frmMain
  Class Server
       Public Shared Sub Main()
        Dim aTcpMessaging As IMessagingSystemFactory = New TcpMessagingSystemFactory()
        Dim anInputChannel As IInputChannel = aTcpMessaging.CreateInputChannel(theIPforLocal & ":" & thePort)
        Dim aStringMessagesFactory As IStringMessagesFactory = New StringMessagesFactory()
        Dim aStringMessageReceiver As IStringMessageReceiver = aStringMessagesFactory.CreateStringMessageReceiver()
        AddHandler aStringMessageReceiver.MessageReceived, AddressOf StringMessageReceived

        aStringMessageReceiver.AttachInputChannel(anInputChannel)
    End Sub

       Private Shared Sub StringMessageReceived()
            Call New frmMM().Show()
       End Sub
  End Class

  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim RelState As Integer = 0
    Call frmMain.Server.Main()
    lblVer.Text = "V.7"
    Pid = 0
  End Sub
End Class

How can i load this up so it doesn't show me that error?

Thanks.

David

Code update

 Class Server
    <STAThread()> Public Sub Main()
        Dim aTcpMessaging As IMessagingSystemFactory = New TcpMessagingSystemFactory()
        Dim anInputChannel As IInputChannel = aTcpMessaging.CreateInputChannel(theIPforLocal & ":" & thePort)
        Dim aStringMessagesFactory As IStringMessagesFactory = New StringMessagesFactory()
        Dim aStringMessageReceiver As IStringMessageReceiver = aStringMessagesFactory.CreateStringMessageReceiver()
        AddHandler aStringMessageReceiver.MessageReceived, AddressOf StringMessageReceived

        aStringMessageReceiver.AttachInputChannel(anInputChannel)
    End Sub

   Private Shared Sub StringMessageReceived()
        Call New frmMM().Show()
   End Sub
 End Class

  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim RelState As Integer = 0
    Call frmMain.Server.Main()   '<-- the error now
    lblVer.Text = "V.7"
    Pid = 0
  End Sub
End Class

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

摘星┃星的人 2025-01-01 15:27:10

出现此问题的原因是 UI 组件是在非 UI 线程上创建的。
这是代码的简化版本,它将因此错误消息而中断。

Imports System.ComponentModel

Public Class Form1
    Private _ThreadedProcesss As ThreadedProcess
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        _ThreadedProcesss = New ThreadedProcess()
        _ThreadedProcesss.Start()
    End Sub

    Public Class ThreadedProcess
        Private _thread As System.Threading.Thread

        Public Sub Start()
            _thread = New System.Threading.Thread(AddressOf ThreadEntryPoint)
            _thread.Name = "Testing thread"
            _thread.Start()
        End Sub

        Private Sub ThreadEntryPoint(status As Object)
            Dim wb As New WebBrowser()
            wb.Dock = DockStyle.Fill
            wb.Visible = True
            wb.DocumentText = "Hello world"
        End Sub
    End Class
End Class

要解决此问题,您只需在创建控件之前返回到 UI 线程即可。

Imports System.ComponentModel

Public Class Form1
    Private _ThreadedProcesss As ThreadedProcess
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        _ThreadedProcesss = New ThreadedProcess(New EventHandler(AddressOf ThreadedProcess_Create))
        _ThreadedProcesss.Start()
    End Sub

    Private Sub ThreadedProcess_Create(sender As Object, e As System.EventArgs)
        If (Me.InvokeRequired) Then
            Me.Invoke(New EventHandler(AddressOf ThreadedProcess_Create), {sender, e})
        Else
            Dim wb As New WebBrowser()
            Me.Controls.Add(wb)
            wb.Dock = DockStyle.Fill
            wb.Visible = True
            wb.DocumentText = "Hello world"
        End If
    End Sub

    Public Class ThreadedProcess
        Private _thread As System.Threading.Thread
        Private _callback As EventHandler

        Public Sub New(callback As EventHandler)
            _callback = callback
        End Sub

        Public Sub Start()
            _thread = New System.Threading.Thread(AddressOf ThreadEntryPoint)
            _thread.Name = "Testing thread"
            _thread.Start()
        End Sub

        Private Sub ThreadEntryPoint(status As Object)
            _callback.Invoke(Me, EventArgs.Empty)
        End Sub
    End Class
End Class

因此,对于您的示例,我相信代码将是:

 Class Server
    Private _callback as EventHandler
    Public Sub Main(callback as EventHandler)
        _callback = callback
        Dim aTcpMessaging As IMessagingSystemFactory = New TcpMessagingSystemFactory()
        Dim anInputChannel As IInputChannel = aTcpMessaging.CreateInputChannel(theIPforLocal & ":" & thePort)
        Dim aStringMessagesFactory As IStringMessagesFactory = New StringMessagesFactory()
        Dim aStringMessageReceiver As IStringMessageReceiver = aStringMessagesFactory.CreateStringMessageReceiver()
        AddHandler aStringMessageReceiver.MessageReceived, AddressOf StringMessageReceived

        aStringMessageReceiver.AttachInputChannel(anInputChannel)
    End Sub

    Private Sub StringMessageReceived()
        _callback.Invoke(Me, EventArgs.Empty)
    End Sub
  End Class

  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      Dim RelState As Integer = 0
      Call frmMain.Server.Main(new EventHandler(AddressOf Server_MessageReceived))   '<-- the error now
      lblVer.Text = "V.7"
      Pid = 0
  End Sub
  Private Sub Server_MessageReceived(sender as Object, e As EventArgs)
      If (Me.InvokeRequired) Then
          Me.Invoke(New EventHandler(AddressOf Server_MessageReceived), {sender, e})
      Else
          Call New frmMM().Show()
      End If
  End Sub
End Class

The problem happens because a UI component is being created on a non-UI thread.
Here is a simplified version of code which will break with this error message.

Imports System.ComponentModel

Public Class Form1
    Private _ThreadedProcesss As ThreadedProcess
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        _ThreadedProcesss = New ThreadedProcess()
        _ThreadedProcesss.Start()
    End Sub

    Public Class ThreadedProcess
        Private _thread As System.Threading.Thread

        Public Sub Start()
            _thread = New System.Threading.Thread(AddressOf ThreadEntryPoint)
            _thread.Name = "Testing thread"
            _thread.Start()
        End Sub

        Private Sub ThreadEntryPoint(status As Object)
            Dim wb As New WebBrowser()
            wb.Dock = DockStyle.Fill
            wb.Visible = True
            wb.DocumentText = "Hello world"
        End Sub
    End Class
End Class

To correct the problem, you just need to get back to the UI thread before creating the control.

Imports System.ComponentModel

Public Class Form1
    Private _ThreadedProcesss As ThreadedProcess
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        _ThreadedProcesss = New ThreadedProcess(New EventHandler(AddressOf ThreadedProcess_Create))
        _ThreadedProcesss.Start()
    End Sub

    Private Sub ThreadedProcess_Create(sender As Object, e As System.EventArgs)
        If (Me.InvokeRequired) Then
            Me.Invoke(New EventHandler(AddressOf ThreadedProcess_Create), {sender, e})
        Else
            Dim wb As New WebBrowser()
            Me.Controls.Add(wb)
            wb.Dock = DockStyle.Fill
            wb.Visible = True
            wb.DocumentText = "Hello world"
        End If
    End Sub

    Public Class ThreadedProcess
        Private _thread As System.Threading.Thread
        Private _callback As EventHandler

        Public Sub New(callback As EventHandler)
            _callback = callback
        End Sub

        Public Sub Start()
            _thread = New System.Threading.Thread(AddressOf ThreadEntryPoint)
            _thread.Name = "Testing thread"
            _thread.Start()
        End Sub

        Private Sub ThreadEntryPoint(status As Object)
            _callback.Invoke(Me, EventArgs.Empty)
        End Sub
    End Class
End Class

So for your example I believe the code would be:

 Class Server
    Private _callback as EventHandler
    Public Sub Main(callback as EventHandler)
        _callback = callback
        Dim aTcpMessaging As IMessagingSystemFactory = New TcpMessagingSystemFactory()
        Dim anInputChannel As IInputChannel = aTcpMessaging.CreateInputChannel(theIPforLocal & ":" & thePort)
        Dim aStringMessagesFactory As IStringMessagesFactory = New StringMessagesFactory()
        Dim aStringMessageReceiver As IStringMessageReceiver = aStringMessagesFactory.CreateStringMessageReceiver()
        AddHandler aStringMessageReceiver.MessageReceived, AddressOf StringMessageReceived

        aStringMessageReceiver.AttachInputChannel(anInputChannel)
    End Sub

    Private Sub StringMessageReceived()
        _callback.Invoke(Me, EventArgs.Empty)
    End Sub
  End Class

  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      Dim RelState As Integer = 0
      Call frmMain.Server.Main(new EventHandler(AddressOf Server_MessageReceived))   '<-- the error now
      lblVer.Text = "V.7"
      Pid = 0
  End Sub
  Private Sub Server_MessageReceived(sender as Object, e As EventArgs)
      If (Me.InvokeRequired) Then
          Me.Invoke(New EventHandler(AddressOf Server_MessageReceived), {sender, e})
      Else
          Call New frmMM().Show()
      End If
  End Sub
End Class
甜中书 2025-01-01 15:27:10

假设您没有使用应用程序框架,则需要使用 STAThread 属性。

例如:

<STAThread()> _
Public Sub Main()

使用应用程序框架的VB应用程序不必担心此属性,因为编译器会自动应用它。

Assuming that you are not using the application framework, you need to decorate your Sub Main with the STAThread attribute.

For example:

<STAThread()> _
Public Sub Main()

VB Applications that use the application framework do not have to worry about this attribute since the compiler will apply it automatically.

南渊 2025-01-01 15:27:10

只是愚蠢地搜索自己,因为我自己并没有真正做任何线程,但我找到了具有相同错误的问题的解决方案。

无论如何,当您尝试在表单上使用对象时,您也会收到此错误:“ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2'无法实例化,因为当前线程不在单线程单元中”(当该表单尚未创建时,其上带有 WebBrowser 控件。

供参考

Just searched myself silly, since I am not really doing any threading myself, but I found a solution to my problem with the same error.

Anyway, you also will get this error: "ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the current thread is not in a single-threaded apartment" when you try to use an object on a form (with the WebBrowser Control on it) when that form has not been created yet.

fyi

坚持沉默 2025-01-01 15:27:10
Class Server
<STAThread()> Public Sub Main()
    Dim aTcpMessaging As IMessagingSystemFactory = New TcpMessagingSystemFactory()
    Dim anInputChannel As IInputChannel = aTcpMessaging.CreateInputChannel(theIPforLocal & ":" & thePort)
    Dim aStringMessagesFactory As IStringMessagesFactory = New StringMessagesFactory()
    Dim aStringMessageReceiver As IStringMessageReceiver = aStringMessagesFactory.CreateStringMessageReceiver()
    AddHandler aStringMessageReceiver.MessageReceived, AddressOf StringMessageReceived

    aStringMessageReceiver.AttachInputChannel(anInputChannel)
End Sub

Private Shared Sub StringMessageReceived()
  Dim t As New Threading.Thread(AddressOf ShowFP)

  t.SetApartmentState(Threading.ApartmentState.STA)
  t.Start()
 End Sub
End Class

Private Shared Sub ShowFP()
  Dim ShowFP As New frmFPVid
  ShowFP.ShowDialog()
End Sub
Class Server
<STAThread()> Public Sub Main()
    Dim aTcpMessaging As IMessagingSystemFactory = New TcpMessagingSystemFactory()
    Dim anInputChannel As IInputChannel = aTcpMessaging.CreateInputChannel(theIPforLocal & ":" & thePort)
    Dim aStringMessagesFactory As IStringMessagesFactory = New StringMessagesFactory()
    Dim aStringMessageReceiver As IStringMessageReceiver = aStringMessagesFactory.CreateStringMessageReceiver()
    AddHandler aStringMessageReceiver.MessageReceived, AddressOf StringMessageReceived

    aStringMessageReceiver.AttachInputChannel(anInputChannel)
End Sub

Private Shared Sub StringMessageReceived()
  Dim t As New Threading.Thread(AddressOf ShowFP)

  t.SetApartmentState(Threading.ApartmentState.STA)
  t.Start()
 End Sub
End Class

Private Shared Sub ShowFP()
  Dim ShowFP As New frmFPVid
  ShowFP.ShowDialog()
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文