Threading - Using Invoke and Begin Invoke

I am fairly new to threading... This is what I do know...
- Other than the main thread all threads need to use Invoke/Begin Invoke to if they are accessing any controls on the UI
- Invoke.IsRequired can be used to check this...
- I can cheat using Control.CheckForIllegalCrossThreadCalls but I would want to get this working the "proper" way and understand what I am doing wrong here...

I cant get the below sub to work... the commented code wont work cause the Sub populates a TreeView control on the form... I tried using delegates but havent used them so I am probably messing up somewhere...

For Each db As Database In selectedServer.Databases
If Not db.IsSystemObject Then tn.Add(New TreeNode(db.Name, 0, 0))
Next
Catch ex As Exception
End Try

' since we've wrapped the treeview with an invoke wrapper to handle marshalling, we can call directly from here
AddTreeViewNode(TreeView1, tn)
Next
Catch smoException As SmoException
MessageBox.Show(smoException.ToString)
Catch exception As Exception
MessageBox.Show(exception.ToString)
Finally
'Me.Cursor = Cursors.Default
End Try
End Sub

Thank you both...
I got the program to work both ways... i.e. using a background worker and without it...
First I tried without it... works as expected... but the UI is not responsive... when I put the DoEvents in the loop that populates the TreeView... the form UI responds better (not as good as I would have liked)...

So I looked at the background worker and implemented that solution thinking I can trigger the ProgressChanged event and have DoEvents inside the event... Also I thought since BackgroundWorker is a built in component it might work better with the UI... but triggering DoEvents through the event is not giving me the desired result...

Also I tracked the CPU usage in both senarios... not even hitting 10%.... using Pentium D 3.00Ghz dual core cpu... any thoughts

I think I am beginning to see the difference between your sample Chaosian and my code... I think my code gets the invoke required and runs on the main thread... instead i could do everything other than populating the treeview in the background thread and just use the delegate to populate the treeview... is this correct? Can you please elaborate if so... I can't seem to wrap my head around this...

Well, the MVP award is for being involved in the community. I think you also need to be nominated by an existing MVP.

In my case, what they looked at for me were being a page editor here, speaking five or six times at various user groups in the last year (including at a code camp), and being involved in my local .NET user group.

Among other things, I've removed your code to set the selected item after loading. This is, in part, because I didn't see it doing anything -- you create a new object then set the treeview seelcted item based on it's name. While I haven't used SMO, I expect this to give you back an empty string as the name.

I am looking at this line : If Not db.IsSystemObject Then tn.Nodes.Add(New TreeNode(db.Name, 0, 0))

smart way of adding child node... I was thinking I will need to add DoEvents in the Sub that adds the nodes since it will be called by the delegate and run on the main thread... and since its being called repeteadly... but it seems to be working fine without it...

If you want better absolute performance, you can build a list of tree nodes in the BackgroundWorker and use AddRange in place of Add in the delegate. However, this looks slower, since the first UI update occurs when you're entirely done.

If you're expecting a lot of servers, you might consider a compromise -- use the technique above, but every N servers (5?) call the delegate.

Thats a good tip... Just some feedback regarding the msgbox... it seems to be working fine... popup without issues... I moved the code to select the local server in the tree node to the RunWorkerCompleted event... works nicely...