I am using the BackgroundProcess thread to do this.
Here's a link to MSDN: http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx
Additional information related to this.
You have three events associated with the BackgroundProcess object: DoWork, ReportProgress, and WorkCompleted.
Now, to use this - and use it with an observable collection - you want to tell the BackgroundProcess object the possibility of PROGRESS REPORT (this is a logical property that I have always explicitly set and also allows cancellation).
Now, to start the process, you call the RunWorkerAsync method. This method has the ability to accept the OBJECT variable in case you need to pass data (if you want more than one value, create a structure that will be passed to RunWorkerAsync).
RunWorkerAsync disconnects from the DoWork event, so the control heads go to your DoWork event handler. Here's the (sanitized) code where I use it from:
Dim dt As System.Data.DataTable dt = da.GetDataTable(sql, System.Data.CommandType.Text, params) For Each row As System.Data.DataRow In dt.Rows If loadQuestionsWorker.CancellationPending Then e.Cancel = True Exit Sub End If Dim item As New DataObject // Assign Item Values backgroundProcessObject.ReportProgress(1, item) Next
What happens here is that I get data from my data layer, and then, until this background process is canceled, I go through the data table, and when I create a new DataObject, I report this object as constructed.
Now, in my ProgressChanged event handler (the ReportProgress method raises the ProgressChanged event), the control is returned to the user interface threads, so I can do things like affect the user interface and add the element I'm reporting an ObservableCollection about.
Finally, in the WorkedCompleted event handler (the corresponding event occurs when the DoWork event handler method is executed before completion). I check if my progress has been canceled (which sometimes means that I want to reset the ObservableCollection), and I may or may not influence the user interface (for example, removing the SEARCH animation.