zoukankan      html  css  js  c++  java
  • .net3.0 中跨线程访问控件

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

        }

    }


  • 相关阅读:
    Java文件操作大全
    struts2整合spring3整合成功但是spring无法注入
    简单理解xFire webservices
    分享几个常用过滤器
    mysql导sql脚本
    开源Jbpm4.4+ssh2+oracle10实现表单、流程自定义开发流程业务
    关于搭建FTP服务器
    chrome标签同步神器插件集装箱!
    记录java接口自动化模板优化
    java之封装日期工具类DateUtils
  • 原文地址:https://www.cnblogs.com/zhouyinhui/p/742134.html
Copyright © 2011-2022 走看看