XAF 如何用其他线程更新对象(转载)
2012-03-25 17:51
288 查看
转自: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();
}
}
}
}
}
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 如何用其他线程更新对象
- C#多线程 使用委托更新UI实例(WP7开发 其他线程中更新UI)(转载)
- 如何在其他线程中修改主UI属性及对象元素特性
- 【转载】ARX如何得到当前CAD打印设备列表及其他打印设置内容
- 有三个线程T1 T2 T3,如何保证他们按顺序执行-转载
- C#新开一个线程取到数据,如何更新到主线程UI上面
- XAF如何通过ControlView自动打开新建的对象实例
- 如何创建线程进程 转载
- 如何在工作线程中更新窗体的UI元素(Dispatcher机制)
- Java中如何安全的结束一个线程(转载)
- 当一个线程进入一个对象的一个synchronized()方法后,其他线程是否可进入此对象的其他方法?
- C# 如何保证对象线程内唯一:数据槽(CallContext)
- MFC中在子线程中如何更新控件的值
- 如何使用 Visual C# 2005 或 Visual C# .NET 通过 DataSet 对象更新数据库
- Android学习笔记-转载两篇关于线程更新UI的方法的文章
- 如何在 Watir 中识别各种 HTML 对象(全/更新到 2007-6-20)
- Android在其他线程中更新UI
- 【转载】C# Winform 跨线程更新UI控件常用方法总结
- 工作线程中如何更新界面
- 如何修改本地代码,并更新到github,及其他使用技巧