0

I want to interact with a control on my main Form (Ui thread). But it is not working, and I need your help!

I use the following code on my main Form:

    delegate sub AddObjectsToFolvDelegate(byref list As List(Of source)) 'delegate

public Sub AddObjectsToFolv(byref value As List(Of source)) 'invoke
    Try
        msgbox(folvsource.IsHandleCreated.ToString) 'Always false, on UI thread true

        If me.InvokeRequired Then 'Always false
            Dim d As New AddObjectsToFolvDelegate(AddressOf AddObjectsToFolv)
            me.invoke(d, value)
        Else
            me.folvsource.AddObjects(value) 'Add Objects to Objectlistview
        End If
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
End Sub

And on the other Class I use the following code to call the invoke:

FrmMain.AddObjectsToFolv(proxysites)

If I check if a invoke is requiered to access the control from a worker thread it always returns false. So if I then check if the handle is created it also returns false (on worker thread).

The thread was started from my main Form after it was compleatly loaded (shown). I also tried to manually create my control with folvSource.CreateControl()

If I call the invoke I get the error message (cause the handle of the control(s) is not created inside my worker thread):

invoke or begininvoke cannot be called on a control until the window handle has been created

I create new threads using the following code with the SmartThreadPool libray on my main Form:

  Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
    'msgbox(import.TestAdd("").ToString)
    dim imp as New Import
    dim MajorPool As New SmartThreadPool
    MsgBox(
        MajorPool.QueueWorkItem(New WorkItemCallback(AddressOf imp.TestAdd), "").GetWorkItemResult.GetResult().
              ToString())
End Sub
Servy
  • 202,030
  • 26
  • 332
  • 449
Lucas
  • 373
  • 1
  • 9
  • 1
    Standard VB.NET bug, `FrmMain` does not do what you hope it does. You *must* use a proper object reference, the type name cannot work when you use it in a worker thread. – Hans Passant Dec 03 '15 at 18:36
  • Can you give me an example please? – Lucas Dec 03 '15 at 18:39
  • 1
    Not from these code snippets. Use CType(Application.OpenForms(0), FrmMain) if you can't think of anything else. – Hans Passant Dec 03 '15 at 18:42
  • So I now invoke to an instance of my FrmMain (using something like [link](http://stackoverflow.com/questions/22366944/do-you-have-to-explicitly-create-instance-of-form-in-vb-net). But I still have the same problem, am I wrong? – Lucas Dec 03 '15 at 19:05
  • @HansPassant this is eventually a standard `VB6 to VB.NET` misunderstanding. BUT if it was that error, it wouldn't compile at all (the method isn't `Shared`). Lucas, can you post something more accurate? the btnAdd is on the form you're trying to access? How `imp` is supposed to know the form? –  Dec 03 '15 at 21:55
  • @HansPassant WHOOPS, sorry i'm totally wrong, i forgot the default instance. –  Dec 03 '15 at 21:59

0 Answers0