2012-11-06 29 views
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 

現在你的代碼。錯誤發生在後臺工作人員嘗試打開表單時。 (在代碼的末尾)

回答

0

好吧,我發現什麼是錯誤。對於有這種錯誤的人,請勿在工作模式下在後臺工作中添加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