Padgett wrote:
Hi Otis,
Sorry - i wasn't asking for this to be built into the framework, I was wasking for an example on how to do what you just described - ie. load the data on a sperate thread then report back when it was done.
Oh, sorry about that
Key for properly doing this is setting up a way to communicate back to the main thread the data is there. I'd do that with a delegate object, which is passed to the new thread and which is p/invoked (no direct call, call Invoke). The delegate is called with the data to fetch as parameter. You can also decide to keep a volatile member variable around where the data is fetched into but that's error prone.
So,
- create delegate to method in mainthread which is used by fetch thread to report data is available. Delegate has to have parameter which is the collection to fetch
- create fetch thread from main thread. Execution of Fetch thread starts
- fetch thread does normal fetching, for example a lot of entity objects using a FetchEntityCollection
- fetch code ends, so thread is effectively done.
- Fetch thread calls delegate, passing in the collection with data to fetch, which is a reference to the entity collection
- main thread's method is called, it receives the data. As it is called using Invoke, the method is ran through the mainthread, and it can safely store the reference to the entity collection in a variable and work with it.
- fetch thread is now completely done and simply ends (routine is exited)