views:

63

answers:

1

My current project is a WPF application with an SQL Server back end.

In WPF, the UI can only be modified by the UI thread. If a UI modification needs to be done on another thread, then the dispatcher object can be called and given an action. Effectively, this is mapping my Delegate to a WM_ message.

Since the linq datacontexts to SQL Server are also single threaded, how could I copy this "Dispatcher" idea from WPF and create a similar object that I can use to marshal requests to my public datacontext to be always from the "Public SQL thread".

I'm guessing I'd need to create a thread at start up which initialises the data contexts and then sleeps until woken by the SqlThread.Invoke() method.

Does anyone know of anything similar to this idea or any materials that may help me do this?

+1  A: 

If you mean a LINQ-to-SQL DataContext, I would advise against this; use DataContexts as a short-lived unit-of-work, then Dispose() it; don't keep it for lots of different purposes (there are issues with stale data, cache growth, threading, concurrency, plus (importantly) how to handle failure / rollback).

Re the bigger picture:

Essentially you are describing a work queue, such as a producer/consumer queue. There are plenty of such around, or they are relatively easy to write (for example, see here or here; just add a loop to dequeue+process items). IIRC .NET 4.0 also includes (in the parallel extensions) such constructs pre-canned.

Marc Gravell
Thanks, the .net 4.0 tip is cool but I don't think I can wait for it.Regarding the short use of datacontexts - thats how the app does it everywhere else, but in the UI I need all the controls to have the same version of the entity whether its old or not.
vanja.