2017-05-30 37 views
0

我想從JSON API中檢索數據並在加載時執行其他任務。 我已經寫代碼的成功在表單模塊vb.net:錯誤從JSON收集數據到不同形式/線程的數據網格

Public Sub aquiredata(mygrid As DataGridView, sql As String, tablename As String) 

    Dim table As DataTable 
    Dim thread1 As New Thread(
     Sub() 
      Try 
       Dim json As String = New System.Net.WebClient().DownloadString("http://ratnt.com/api.php/" & _ 
     tablename & "?query=" & Chr(34) & sql & Chr(34) & "&token=" & My.Settings.token) ' 
        table = JsonConvert.DeserializeObject(Of DataTable)(json) 
        MsgBox("table done") 
       Finally 
        Invoke(Sub() 
          mygrid.DataSource = table 
         End Sub) 
       End Try 
      End Sub 
     ) 
     thread1.Start() 
    End Sub 

但是當我從另一種形式與形式的DataGrid中調用它,它顯示

"An unhandled exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll 

Additional information: Invoke or BeginInvoke cannot be called on a control until the window handle has been created." 

(PS我與BackgroundWorker的嘗試,但原因是我避免了它對每一個表格都是重複的,因爲解析結果只能通過RunWorkerCompleted事件完成。)

回答

0

我錯過了很簡單的東西 這是消息中的基礎...

Invoke or BeginInvoke cannot be called on a control until the window handle has been created 

所以只是略有變化 mygrid.invoke,而不是隻invoke是 工作。

謝謝