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

转载 2012年03月25日 17:51:38

转自: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();
                }
            }
        }
    }
}


相关文章推荐

XAF之点击Navigation Control弹出新建对象窗口

当单击navigation control的item时,执行New(NewObjectViewController.NewObjectAction)按钮。      先新建一个DemoIss...
  • qjpcpu
  • qjpcpu
  • 2012年03月30日 20:30
  • 956

XAF之 未解决——无法显示包含nonpersistent对象的ListView

当尝试在ListView中显示NonPersistent Obejct时,会报告objectKeyAsString参数为null,显示一个红色大叉。 查看输出如下: 15.05.12 10...
  • qjpcpu
  • qjpcpu
  • 2012年05月15日 01:31
  • 1018

MTA客户跨线程调用STA对象

  • 2014年08月26日 20:31
  • 28KB
  • 下载

STA客户跨线程调用STA对象

  • 2014年08月25日 23:55
  • 29KB
  • 下载

【重大更新】DevExpress v16.2新版亮点(XAF篇)

用户界面套包DevExpress v16.2终于正式发布,本站将以连载的形式为大家介绍各版本新增内容。.NET应用程序开发框架DevExpress eXpressApp Framework v16.2...

XAF 架构(转载)

转自:http://www.cnblogs.com/Tonyyang/archive/2011/01/07/1929528.html 使用 eXpressApp Framework ...
  • qjpcpu
  • qjpcpu
  • 2012年03月25日 17:56
  • 948

Android_子线程创建消息队列更新UI

  • 2015年12月07日 00:16
  • 2.53MB
  • 下载

封装多媒体定时器类和时间核心对象线程类

背景:虽然Win95下可视化开发工具如VC、Delphi、C++   Builder等都有专用的定时器控件Timer,而且使用很方便,可以实现一定的定时功能,但最小计时精度仅为55ms,且定时器消息在...
  • Augusdi
  • Augusdi
  • 2013年09月03日 14:25
  • 1349

WPF 使用线程更新UI

  • 2014年11月30日 15:47
  • 61KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:XAF 如何用其他线程更新对象(转载)
举报原因:
原因补充:

(最多只允许输入30个字)