2017-05-30 1 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 

に成功したコードを書かれているが、私はそのフォームのデータグリッドで別のフォームからそれを呼んでいる間、それは示して

"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." 

(私はBackgroundWorkerのを試してみましたPSが、なぜなら、解析結果はRunWorkerCompletedイベントでのみ完了しているため、すべてのフォームで繰り返しているのを避けたからです。)

答えて

0

メッセージの根底にある非常に単純なもの を逃しました...

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

だからわずかな変化 mygrid.invoke代わりのinvokeのみ 取り組んでいます。

ありがとうございました

関連する問題