XAF 如何用其他线程更新对象
2010-08-03 09:11
267 查看
// Developer Express Code Central Example:
// How to update a View object from another thread.
//
// It is well known that most Windows Forms are not thread safe, and all changes to
// these controls should run (be marshaled on to) the thread processing windows
// events (the main UI thread). Worker threads are often used to do lengthy
// calculations or process asynchronously messaged events, allowing the UI to react
// to the user while these tasks are operating. This example shows how bypass this
// limitation by updating the object directly in the database, and then refreshing
// the view to reload the current object. This is a little bit tricky, but works
// quite well. Another known workaround, is marshaling actual work to the main UI
// thread, for instance in the main form. You can learn more about this in MSDN,
// Google, etc. For instance, here is a good article:
// http://weblogs.asp.net/justin_rogers/pages/126345.aspx //
// You can find sample updates and versions for different programming languages here:
// http://www.devexpress.com/example=E1394
using System.ComponentModel;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Actions;
using System.Threading;
using DevExpress.Xpo;
namespace WinSolution.Module.Win {
public partial class ChangeCurrentObjectFromAnotherThreadViewController : ViewController {
public ChangeCurrentObjectFromAnotherThreadViewController() {
InitializeComponent();
RegisterActions(components);
}
private void simpleAction1_Execute(object sender, SimpleActionExecuteEventArgs e) {
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += new DoWorkEventHandler(worker_DoWork);
worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
worker.RunWorkerAsync();
}
void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
View.ObjectSpace.Refresh();
}
void worker_DoWork(object sender, DoWorkEventArgs e) {
Thread.Sleep(1000);
DomainObject1 obj = (DomainObject1)View.CurrentObject;
if (obj != null) {
using (UnitOfWork uow = new UnitOfWork(View.ObjectSpace.Session.DataLayer)) {
DomainObject1 copy = uow.GetObjectByKey<DomainObject1>(obj.Oid, true);
copy.Name = "test";
uow.CommitChanges();
}
}
}
}
}
// How to update a View object from another thread.
//
// It is well known that most Windows Forms are not thread safe, and all changes to
// these controls should run (be marshaled on to) the thread processing windows
// events (the main UI thread). Worker threads are often used to do lengthy
// calculations or process asynchronously messaged events, allowing the UI to react
// to the user while these tasks are operating. This example shows how bypass this
// limitation by updating the object directly in the database, and then refreshing
// the view to reload the current object. This is a little bit tricky, but works
// quite well. Another known workaround, is marshaling actual work to the main UI
// thread, for instance in the main form. You can learn more about this in MSDN,
// Google, etc. For instance, here is a good article:
// http://weblogs.asp.net/justin_rogers/pages/126345.aspx //
// You can find sample updates and versions for different programming languages here:
// http://www.devexpress.com/example=E1394
using System.ComponentModel;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Actions;
using System.Threading;
using DevExpress.Xpo;
namespace WinSolution.Module.Win {
public partial class ChangeCurrentObjectFromAnotherThreadViewController : ViewController {
public ChangeCurrentObjectFromAnotherThreadViewController() {
InitializeComponent();
RegisterActions(components);
}
private void simpleAction1_Execute(object sender, SimpleActionExecuteEventArgs e) {
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += new DoWorkEventHandler(worker_DoWork);
worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
worker.RunWorkerAsync();
}
void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
View.ObjectSpace.Refresh();
}
void worker_DoWork(object sender, DoWorkEventArgs e) {
Thread.Sleep(1000);
DomainObject1 obj = (DomainObject1)View.CurrentObject;
if (obj != null) {
using (UnitOfWork uow = new UnitOfWork(View.ObjectSpace.Session.DataLayer)) {
DomainObject1 copy = uow.GetObjectByKey<DomainObject1>(obj.Oid, true);
copy.Name = "test";
uow.CommitChanges();
}
}
}
}
}
相关文章推荐
- XAF 如何用其他线程更新对象(转载)
- 如何在其他线程中修改主UI属性及对象元素特性
- WPF [调用线程无法访问此对象,因为另一个线程拥有该对象。] 解决方案以及如何实现字体颜色的渐变
- Android 在其他线程中更新UI线程的解决方法
- 搭建个人博客之 Github + hexo: 如何在其他电脑上更新博客
- Android中通过其他线程更新主线程UI
- 论mongo-connector如何将MongoDB中的json数组和嵌套对象更新至Solr引擎
- wpf-由于其他线程拥有此对象,因此调用线程无法对其进行访问。
- android-如何在子线程中更新ui
- android如何在子线程中更新UI
- XAF如何通过ControlView自动打开新建的对象实例
- MFC如何在主线程中向view类发消息或是其他类向主框架类发消息
- 工作线程中如何更新界面
- 后台线程处理数据,如何实时更新UI(datagridview)----多线程同步问题
- 【转】WPF [调用线程无法访问此对象,因为另一个线程拥有该对象。] 解决方案以及如何实现字体颜色的渐变
- MFC中在子线程中如何更新控件的值
- 如何给一个正在跑的线程传递东西,其实只要在其他类里,得到该线程的引用 就可以了<EOM>
- 如何从List,ArrayList集合中删除对象,或者其他数据
- Android 在其他线程中更新UI 线程
- android 在子线程中如何把自定义对象传到handler中处理