创建类似于 SQL DataContext 的 Dispatcher 的东西
我当前的项目是一个带有 SQL Server 后端的 WPF 应用程序。
在WPF中,UI只能由UI线程修改。如果需要在另一个线程上完成 UI 修改,则可以调用调度程序对象并给出一个操作。实际上,这将我的委托映射到 WM_ 消息。
由于 SQL Server 的 linq 数据上下文也是单线程的,因此我如何从 WPF 复制此“调度程序”想法并创建一个类似的对象,我可以使用该对象将请求编组到我的公共数据上下文,使其始终来自“公共 SQL 线程”。
我猜我需要在启动时创建一个线程来初始化数据上下文,然后休眠直到被 SqlThread.Invoke() 方法唤醒。
有谁知道与此想法类似的任何内容或可以帮助我做到这一点的任何材料?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您指的是 LINQ-to-SQL
DataContext
,我建议不要这样做;使用 DataContext 作为短期工作单元,然后使用 Dispose() 进行处理;不要将其用于许多不同的目的(存在陈旧数据、缓存增长、线程、并发性以及(重要的是)如何处理故障/回滚的问题)。回顾更大的图景:
本质上,您正在描述一个工作队列,例如生产者/消费者队列。周围有很多这样的,或者它们相对容易编写(例如,请参见 此处或此处;只需添加循环出队+处理项目)。 IIRC .NET 4.0 还包括(在并行扩展中)此类预先封装的构造。
If you mean a LINQ-to-SQL
DataContext
, I would advise against this; useDataContext
s as a short-lived unit-of-work, thenDispose()
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.