跨线程UI组件调用 [英] Cross thread UI component call

查看:259
本文介绍了跨线程UI组件调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是处理跨线程操作以适当的方式?



我应该用一个新的属性名称,像EditValueThreadSafe,而不是压倒一切的EditValue?我不认为存在与变化的EditValue的实施问题,如基础属性被称为无关。

 命名空间MyApplication.Components使用System.Windows.Forms的
{
;

///<总结>
///线程安全的实现DevExpress.XtraEditors.ComboBoxEdit类。
///< /总结>
公共类ComboBoxEditThreadSafe:DevExpress.XtraEditors.ComboBoxEdit
{
///<总结>
///获取或设置编辑值。
///< /总结>
///<价值>在编辑值< /值>
公众覆盖对象的EditValue
{
得到
{
返回base.EditValue;
}


{
如果(this.InvokeRequired)
{
this.Invoke(新MethodInvoker(代表
{
this.SetEditValue(价值);
}));
}
,否则
{
this.SetEditValue(值);
}
}
}

///<总结>
///设置编辑值。
///< /总结>
///< PARAM NAME =值>该值< /参数>
私人无效SetEditValue(对象的值)
{
base.EditValue =价值;
}
}
}


解决方案

您也可以委托给该做的工作,并在该方法中,如果错误的线程上(的BeginInvoke返回true)另一种方法,然后再回到调用相同的方法。 。这样做是无需重复的代码



 公共类ComboBoxEditThreadSafe:DevExpress.XtraEditors.ComboBoxEdit 
{
公共覆盖对象的EditValue
{
得到
{
返回base.EditValue;
}

{
的SetValue(值);
}
}



私人无效委托SetValueDlg(对象valeu);
私人无效的SetValue(对象的值)
{
如果(this.InvokeRequired)
this.BeginInvoke(
(SetValueDlg)的SetValue,//调用本身,而取决于正确的线程
新的对象[] {值});
,否则

base.editValue =价值;

}
}

您也可以使用Action( )泛型类以消除需要建立明确的委托类...

 公共类ComboBoxEditThreadSafe:DevExpress.XtraEditors.ComboBoxEdit 
{
公众覆盖对象的EditValue
{
{返回base.EditValue; }
集合{的SetValue(值); }
}

私人无效的SetValue(对象的值)
{
如果(this.InvokeRequired)
this.BeginInvoke(
新动作< ;对象>(的SetValue),//调用本身,而是对正确的线程
新的对象[] {值});
,否则
base.editValue =价值;

}



}


Is this an appropriate way of handling cross-thread operations?

Should I use a new property name, something like "EditValueThreadSafe" instead of overriding "EditValue"? I don't think there is an issue with the changes to the implementation of EditValue, as the base property is called regardless.

namespace MyApplication.Components
{
    using System.Windows.Forms;

    /// <summary>
    /// Thread-safe implementation of the DevExpress.XtraEditors.ComboBoxEdit class.
    /// </summary>
    public class ComboBoxEditThreadSafe : DevExpress.XtraEditors.ComboBoxEdit
    {
        /// <summary>
        /// Gets or sets the edit value.
        /// </summary>
        /// <value>The edit value.</value>
        public override object EditValue
        {
            get
            {
                return base.EditValue;
            }

            set
            {
                if (this.InvokeRequired)
                {
                    this.Invoke(new MethodInvoker(delegate
                    {
                        this.SetEditValue(value);
                    }));
                }
                else
                {
                    this.SetEditValue(value);
                }
            }
        }

        /// <summary>
        /// Sets the edit value.
        /// </summary>
        /// <param name="value">The value.</param>
        private void SetEditValue(object value)
        {
            base.EditValue = value;
        }
    }
}

解决方案

You can also delegate to another method that does the work, and in that method, if on the wrong thread, (BeginInvoke returns true), then call the same method back again. Doing that that eliminates the need to duplicate code.

public class ComboBoxEditThreadSafe : DevExpress.XtraEditors.ComboBoxEdit    
{       
   public override object EditValue        
   {            
       get            
       {                             
            return base.EditValue;            
       }            
       set  
       {                
         SetValue(value);
       }
   }



    private void delegate SetValueDlg(object valeu);
    private void SetValue(object value)
    {
        if (this.InvokeRequired)
             this.BeginInvoke(
                 (SetValueDlg)SetValue,  // calls itself, but on correct thread
                 new object[] { value });
        else

              base.editValue = value;  

    }
}

You can also use the Action() generic class to eliminate need to create explicit delegate class...

   public class ComboBoxEditThreadSafe : DevExpress.XtraEditors.ComboBoxEdit    
{       
   public override object EditValue        
   {            
       get {  return base.EditValue;   }            
       set { SetValue(value); }
   }

   private void SetValue(object value)
   {
       if (this.InvokeRequired)
           this.BeginInvoke(
               new Action<object>(SetValue),  // calls itself, but on correct thread
               new object[] { value });
       else                
              base.editValue = value;  

   }

}

这篇关于跨线程UI组件调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆