1

我正在为这个应用程序的新版本创建一个服务器检查点。当我的应用程序尝试打开我的主窗体时,我遇到了这个错误。

创建表单时出错。有关详细信息,请参阅 Exception.InnerException。错误是:ActiveX 控件 '6bf52a52-394a-11d3-b153-00c04f79faa6' 无法实例化,因为当前线程不在单线程单元中。

我不确定这是什么,因为它类似于通常的表单,除了此表单用作启动画面。当我删除启动画面时,表单会正常打开所有插件和模块。这是我使用的代码的一部分

Public Class example_form
    Public Function servercheck() As Boolean
        Dim objUrl As New System.Uri("http://google.com")
        Dim objWebReq As System.Net.WebRequest
        objWebReq = System.Net.WebRequest.Create(objUrl)
        Dim objresp As System.Net.WebResponse

        Try
            objresp = objWebReq.GetResponse
            objresp.Close()
            objresp = Nothing
            Return True

        Catch ex As Exception
            objresp = Nothing
            objWebReq = Nothing
            Return False
        End Try
    End Function


    Private Sub Form4_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        BackgroundWorker1.RunWorkerAsync()
        Control.CheckForIllegalCrossThreadCalls = False

    End Sub

    Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        If servercheck() = True Then
            Form1.Show()
            Me.Hide()
            BackgroundWorker1.CancelAsync()
        Else : PictureBox1.Image = My.Resources._12383u9
            MsgBox("some text here", MsgBoxStyle.Critical)
            End
        End If
    End Sub
End Class

现在你有了代码。后台工作人员尝试打开表单时出现错误。(在代码末尾)

4

1 回答 1

0

好的,我发现了错误。有这个错误的请不要在工作模式的backgroundworker中添加form.show()命令。当后台工作人员完成时,应该加载表单。所以正确的代码是这样的

Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
              If servercheck() = True Then
            Me.Hide()
            BackgroundWorker1.CancelAsync()
        Else : PictureBox1.Image = My.Resources._12383u9
            MsgBox("Some text here", MsgBoxStyle.Critical)
            End
        End If
    End Sub

    Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
        Form1.Show()
    End Sub 
于 2012-11-07T17:29:52.570 回答