In .Net 4.5 I have lists which contain objects that are time-consuming to load. A list of 5000 objects takes about 5 seconds to load, so for each loops take quite a long time to execute.
Since the initialisation of the objects is what takes all the time I was wondering if there is a type of list, or a way to make a list like this, where the objects are loaded progressively when they are needed.
Something like a list which would know how many objects there are, but which only instantiates objects when they are retrieved. Does this exist? Or is it possible to make a list like that?
I am looking for advice as to how to get this done (if it is doable), so bot C# or VB.Net code would be fine. Hence I added both tags.
EDIT
I should add that I have tried playing with lazy-loading, but that only seems to defer the loading of the list, which is not really a solution.
EDIT
Here is what I am using now. Note that I removed my lame attempts to use lazy loading and reverted back to the way I used to do it yesterday (which is effectively lazy loading without a Lazy
object)
Public Class SISOverlayItemList : Inherits SISBaseObject : Implements IEnumerable(Of SISOverlayItem)
Default Public ReadOnly Property Item(ByVal Index As Integer) As SISOverlayItem
Get
Return New SISOverlayItem(Me.List(Index).ID, Me.Parent)
End Get
End Property
Public ReadOnly Property Items As List(Of SISOverlayItem)
Get
Dim output As New List(Of SISOverlayItem)
Call Me.List.Items.AsParallel.ForAll(Sub(i) output.Add(New SISOverlayItem(i.ID, Me.Parent))
Return output
End Get
End Property
Public ReadOnly Property Parent As SISOverlay
Get
Return Me._Parent
End Get
End Property
Private ReadOnly Property List As SISList
Get
If Me._List Is Nothing Then
Me._List = Me.Application.Lists(Me.Name)
End If
Call Me.Refresh()
Return Me._List
End Get
End Property
Private _List As SISList
Private _Parent As SISOverlay
Public Sub New(ByVal Parent As SISOverlay)
Me._Parent = Parent
Me._Application = Parent.Application
Me._Name = String.Format("Overlay_{0}_Items", Parent.Name)
Call Me.Refresh()
End Sub
Public Sub Refresh()
Call Me.Application.Lists(Me.Name).ScanOverlay(Me.Parent)
End Sub
Public Function GetEnumerator() As IEnumerator(Of SISOverlayItem) Implements IEnumerable(Of SISOverlayItem).GetEnumerator
Return Me.Items.GetEnumerator()
End Function
Private Function GetEnumerator1() As IEnumerator Implements IEnumerable.GetEnumerator
Return Me.GetEnumerator
End Function
End Class
The Items
property which converts all objects from List
to items of the current list (SISOverlayItem
) is what is taking so long. What is taking all the time in the conversion if that the ID
parameter which is needed to instantiate an SISOverlayItem
requires an API call to the Application
object stored in each item of List
.
If it were possible to execute these calls in small batches the or something it should remove the long delay required to instantiate every object in the list.