2013-03-10 68 views
0

如何修改textbox控件或numbericUpDown控件的文本值或数值或将项目和/或子项添加到listview控件或更新线程内的进度栏,而无需创建多个定义,如新功能和新委托?更新控制线程安全

回答

1

我创建了一个线程安全的扩展函数集,用于我创建的一个应用程序所需的一组控件,它需要大量线程更新到窗体。该类直接将方法添加到控件中,因此使访问控件线程安全需要非常少的代码更改。只需将此类添加到您的项目中即可。

using System; 
using System.Collections.Generic; 
using System.Text; 
using System.Windows.Forms; 
using System.Drawing; 

namespace System.Windows.Forms 
{ 
    public static class TSFormExtenders 
    { 
    #region Control 
    public static void SetEnabledTS(this Control x, bool s) 
    { 
     if (x.InvokeRequired) 
     { 
     x.Invoke(new EventHandler(delegate(object o, EventArgs a) 
     { 
      x.SetEnabledTS(s); 
     })); 
     } 
     else 
     { 
     x.Enabled = s; 
     } 
    } 

    public static bool GetEnabledTS(this Control x, bool def = false) 
    { 
     if (x.InvokeRequired) 
     { 
     bool m_ret = def; 
     x.Invoke(new EventHandler(delegate(object o, EventArgs a) 
     { 
      m_ret = x.GetEnabledTS(); 
     })); 
     return m_ret; 
     } 
     else 
     { 
     return x.Enabled; 
     } 
    } 

    public static void SetTextTS(this Control x, String s) 
    { 
     if (x.InvokeRequired) 
     { 
     x.Invoke(new EventHandler(delegate(object o, EventArgs a) 
     { 
      x.SetTextTS(s); 
     })); 
     } 
     else 
     { 
     x.Text = s; 
     } 
    } 

    public static String GetTextTS(this Control x, String def = "") 
    { 
     if (x.InvokeRequired) 
     { 
     String m_ret = def; 
     x.Invoke(new EventHandler(delegate(object o, EventArgs a) 
     { 
      m_ret = x.GetTextTS(); 
     })); 
     return m_ret; 
     } 
     else 
     { 
     return x.Text; 
     } 
    } 

    public static void SetVisibleTS(this Control x, bool s) 
    { 
     if (x.InvokeRequired) 
     { 
     x.Invoke(new EventHandler(delegate(object o, EventArgs a) 
     { 
      x.SetVisibleTS(s); 
     })); 
     } 
     else 
     { 
     x.Visible = s; 
     } 
    } 

    public static bool GetVisibleTS(this Control x, bool def = true) 
    { 
     if (x.InvokeRequired) 
     { 
     bool m_ret = def; 
     x.Invoke(new EventHandler(delegate(object o, EventArgs a) 
     { 
      m_ret = x.GetVisibleTS(); 
     })); 
     return m_ret; 
     } 
     else 
     { 
     return x.Visible; 
     } 
    } 

    public static void SetSizeTS(this Control x, Size s) 
    { 
     if (x.InvokeRequired) 
     { 
     x.Invoke(new EventHandler(delegate(object o, EventArgs a) 
     { 
      x.SetSizeTS(s); 
     })); 
     } 
     else 
     { 
     x.Size = s; 
     } 
    } 

    public static Size GetSizeTS(this Control x) 
    { 
     if (x.InvokeRequired) 
     { 
     Size m_ret = new Size(); 
     x.Invoke(new EventHandler(delegate(object o, EventArgs a) 
     { 
      m_ret = x.GetSizeTS(); 
     })); 
     return m_ret; 
     } 
     else 
     { 
     return x.Size; 
     } 
    } 
    #endregion 

    #region CheckBox 
    public static void SetCheckedTS(this CheckBox x, bool s) 
    { 
     if (x.InvokeRequired) 
     { 
     x.Invoke(new EventHandler(delegate(object o, EventArgs a) 
     { 
      x.SetCheckedTS(s); 
     })); 
     } 
     else 
     { 
     x.Checked = s; 
     } 
    } 

    public static bool GetCheckedTS(this CheckBox x) 
    { 
     if (x.InvokeRequired) 
     { 
     bool m_ret = false; 
     x.Invoke(new EventHandler(delegate(object o, EventArgs a) 
     { 
      m_ret = x.GetCheckedTS(); 
     })); 
     return m_ret; 
     } 
     else 
     { 
     return x.Checked; 
     } 
    } 
    #endregion 

    #region NumericUpDown 
    public static void SetValueTS(this NumericUpDown x, Decimal s) 
    { 
     if (x.InvokeRequired) 
     { 
     x.Invoke(new EventHandler(delegate(object o, EventArgs a) 
     { 
      x.SetValueTS(s); 
     })); 
     } 
     else 
     { 
     x.Value = s; 
     } 
    } 

    public static Decimal GetValueTS(this NumericUpDown x) 
    { 
     if (x.InvokeRequired) 
     { 
     Decimal m_ret = 0; 
     x.Invoke(new EventHandler(delegate(object o, EventArgs a) 
     { 
      m_ret = x.GetValueTS(); 
     })); 
     return m_ret; 
     } 
     else 
     { 
     return x.Value; 
     } 
    } 

    public static void SetMinTS(this NumericUpDown x, Decimal s) 
    { 
     if (x.InvokeRequired) 
     { 
     x.Invoke(new EventHandler(delegate(object o, EventArgs a) 
     { 
      x.SetMinTS(s); 
     })); 
     } 
     else 
     { 
     x.Minimum = s; 
     } 
    } 

    public static void SetMaxTS(this NumericUpDown x, Decimal s) 
    { 
     if (x.InvokeRequired) 
     { 
     x.Invoke(new EventHandler(delegate(object o, EventArgs a) 
     { 
      x.SetMaxTS(s); 
     })); 
     } 
     else 
     { 
     x.Maximum = s; 
     } 
    } 
    #endregion 

    #region ProgressBar 
    public static void SetValueTS(this ProgressBar x, Int32 s) 
    { 
     if (x.InvokeRequired) 
     { 
     x.Invoke(new EventHandler(delegate(object o, EventArgs a) 
     { 
      x.SetValueTS(s); 
     })); 
     } 
     else 
     { 
     x.Value = s; 
     } 
    } 

    public static Int32 GetValueTS(this ProgressBar x) 
    { 
     if (x.InvokeRequired) 
     { 
     Int32 m_ret = 0; 
     x.Invoke(new EventHandler(delegate(object o, EventArgs a) 
     { 
      m_ret = x.GetValueTS(); 
     })); 
     return m_ret; 
     } 
     else 
     { 
     return x.Value; 
     } 
    } 

    public static void SetMinTS(this ProgressBar x, Int32 s) 
    { 
     if (x.InvokeRequired) 
     { 
     x.Invoke(new EventHandler(delegate(object o, EventArgs a) 
     { 
      x.SetMinTS(s); 
     })); 
     } 
     else 
     { 
     x.Minimum = s; 
     } 
    } 

    public static void SetMaxTS(this ProgressBar x, Int32 s) 
    { 
     if (x.InvokeRequired) 
     { 
     x.Invoke(new EventHandler(delegate(object o, EventArgs a) 
     { 
      x.SetMaxTS(s); 
     })); 
     } 
     else 
     { 
     x.Maximum = s; 
     } 
    } 
    #endregion 

    #region ListView 
    public static void AddItemTS(this ListView x, ListViewItem s) 
    { 
     if (x.InvokeRequired) 
     { 
     x.Invoke(new EventHandler(delegate(object o, EventArgs a) 
     { 
      x.AddItemTS(s); 
     })); 
     } 
     else 
     { 
     x.Items.Add(s); 
     } 
    } 

    public static void AddItemTS(this ListViewItem x, System.Windows.Forms.ListViewItem.ListViewSubItem s) 
    { 
     if (x.ListView.InvokeRequired) 
     { 
     x.ListView.Invoke(new EventHandler(delegate(object o, EventArgs a) 
     { 
      x.AddItemTS(s); 
     })); 
     } 
     else 
     { 
     x.SubItems.Add(s); 
     } 
    } 
    #endregion 
    } 
} 
+0

我使用[的SynchronizationContext](http://msdn.microsoft.com/en-us/library/system.threading.synchronizationcontext.aspx)(后面的小包装器),其抽象一切出并易于呼叫,以便在呼叫站点保持同步,而无需额外的样板。大多数时候,我发现操作应该一起完成 - 所以将它留在呼叫站点更清洁,更高效,并允许根据需要选择同步或异步行为。 – 2013-03-10 11:31:41