教程集 www.jiaochengji.com
教程集 >  脚本编程  >  Asp.net  >  正文 asp.net3.0中进行跨线程访问的替代方法

asp.net3.0中进行跨线程访问的替代方法

发布时间:2014-07-20   编辑:jiaochengji.com
asp.net3.0中进行跨线程访问的替代方法

用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);
 }
 }
}

您可能感兴趣的文章:
asp.net3.0中进行跨线程访问的替代方法
jQuery 跨域访问问题解决方法
JQuery Ajax 跨域访问的解决方案
java中使用Cookie替代Session解决跨域Session失效问题
Java多线程原理及ThreadLocal多线程实例详解
经典Java线程面试题70道
使用html5可以干什么?Html5的优势和劣势(总结)
Python中进程与线程的区别是什么
一文带你读懂Python线程
php单线程怎么理解

[关闭]
~ ~