Yojo网页设计

yojoPHP.NETPhotoshopCSSASPPhotoshopDreamweaverFlashFireworksCoreldrawillustratorJSPSEOSQL
·ASP基础·ASP技巧·ASP实例·Asp.Net基础·Asp.Net技巧·Asp.Net实例·PHP·JSP·编程杂谈·数据库编程

热门点击

more...
您现在的位置: yojo网页设计 >> 编程开发 >> ASP.net教程 >> Asp.Net基础教程 >> 正文

介绍.Net3.0 中跨线程访问控件

更新时间:2007-11-18 19:08:29 文章来源:互联网 点击:

  这两天用WPF做一个项目的UI部分时,发现跨线程地访问了UI控件,自然地报异常了。当时找了半天也没在控件中找到InvokeRequired属性和Invoke方法,郁闷之极.....最后发现在.net3.0中,这有所改变了。

  替代InvokeRequired的方法是DispatcherObject.CheckAccess()或DispatcherObject.VerifyAccess()方法,用于指示当前线程是否可以直接访问控件。

  替代Invoke的方法是DispatcherObject.Dispatcher.BeginInvoke(...)方法。

  参考代码:


  // Uses the DispatcherObject.CheckAccess method to determine if
  // the calling thread has access to the thread the UI object is on
  private void TryToUpdateButtonCheckAccess(object uiObject)
  {
   Button theButton = uiObject as Button;
  
   if (theButton != null)
   {
   // Checking if this thread has access to the object
   if(theButton.CheckAccess())
   {
   // This thread has access so it can update the UI thread
   UpdateButtonUI(theButton);
   }
   else
   {
   // This thread does not have access to the UI thread
   // Pushing update method on the Dispatcher of the UI thread
   theButton.Dispatcher.BeginInvoke(DispatcherPriority.Normal,
   new UpdateUIDelegate(UpdateButtonUI), theButton);
   }
   }
  }

友情链接首页文字链接要求:PR≥3,搜索引擎正常收录,开通一年以上,内容健康充实的站点!申请...

Copyright(C) 2005-2008 518web.net,All Rights Reserve
Email:yojo.x@msn.com | 在线QQ:2306380 53614197| [浙ICP备08009643号]