2013-03-10 79 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