Search code examples
vb.netwinformsclassobjectlistview

Can't Update ObjectListView On Main Form From Class


I am hoping someone can help me with an issue I have:

I have a main form with an ObjectListView control on it. When I add objects to the control via code on the Main form, everything works fine, however....

I have a Class that does some processing. This class kicks off a ThreadPool to process incoming information, and once complete, I am trying to update the ObjectListView stored on the Main Form.

The weird thing is I get absolutely no errors and the debugger steps through fine, it's just that nothing ever gets populated on the control via that Class.

I don't know what I'm doing wrong...

So this gives me nothing:

Dim _activityItem As New ActivityItem
frmMain.lsvActivity.AddObject(_activityItem)

Yet it works fine on frmMain

Update I've turned off the ThreadPooling and it still doesn't update the ObjectListView control.

Update 2 Just to try and make things a little clearer, I'll explain how may app works (briefly):

  • When my Main Form loads, I start a thread to listen for incoming messages
  • Any population made to the ObjectListView is fine from the Main Form
  • When a message is received, it is passed to a MessageHandling Class to decode
  • Once decoded, it is passed into a Thread Pool for processing
  • Once all processing is complete, a status is given to the user through the ObjectListView but still within the MessageHandling Class (using the code snippet explained above)
  • The application then repeats the entire process for each incoming message or until exited

Thanks


Solution

  • Try this.

    frmMain.Invoke(Sub() frmMain.lsvActivity.AddObject(_activityItem))
    

    You shouldn't access the UI controls directly from any threads other than the UI thread.
    Control.Invoke executes the specified delegate on the UI thread.