XAF 如何用其他线程更新对象(转载)

转自:http://www.cnblogs.com/Tonyyang/archive/2010/08/03/1790950.html

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->// 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();
                }
            }
        }
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值