2012-01-06 36 views
0

我寫它可以異步運行的功能,那麼,事實是該類非常難看異步類,見下圖:我寫了一個異步類,但我不kown如何優化它

using System; 
using System.Windows.Forms; 

namespace AsyncLibery 
{ 
public class AsyncLib 
{ 
    public AsyncLib() { } 

    public AsyncLib(Object myObject) 
    { 
     this.MyObject = myObject; 
    } 

    public Object MyObject { get; set; } 

    /// <summary> 
    /// No Parameter,WithOut ReturnValue 
    /// </summary> 
    /// <param name="actionFunction">the function needed to be delegated</param> 
    public void Async(Action actionFunction) 
    { 
     Form form = (MyObject as Form); 
     form.Invoke((Action)(() => actionFunction())); 
    } 

    /// <summary> 
    /// No parameter, With returnValue 
    /// </summary> 
    /// <param name="funcFunction">the function needed to be delegated</param> 
    /// <returns>return object type</returns> 
    public object AsyncWithReturnValue(Func<object> funcFunction) 
    { 
     object returnValue = null; 
     Form form = (MyObject as Form); 
     form.Invoke(new Func<object>(delegate() 
     { 
      returnValue = funcFunction(); 
      return returnValue; 
     })); 
     return returnValue; 
    } 

    /// <summary> 
    /// One Parameter, With ReturnValue 
    /// </summary> 
    /// <param name="funcFunction">the function needed to be delegated</param> 
    /// <param name="inputValue">the input parameter</param> 
    /// <returns></returns> 
    public object AsyncWithReturnValue(Func<object, object> funcFunction, object inputValue) 
    { 
     object returnValue = null; 
     Form form = (MyObject as Form); 
     form.Invoke(new Func<object,object>(delegate(object _object) 
     { 
      returnValue = funcFunction(_object); 
      return returnValue; 
     }),inputValue); 
     return returnValue; 
    } 

    /// <summary> 
    /// Two Parameters , With ReturnValue 
    /// </summary> 
    /// <param name="funcFunction">the function needed to be delegated</param> 
    /// <param name="inputValue1">the first input parameter</param> 
    /// <param name="inputValue2">this second input parameter</param> 
    /// <returns></returns> 
    public object AsyncWithReturnValue(Func<object, object, object> funcFunction, object inputValue1, object inputValue2) 
    { 
     object returnValue = null; 
     Form form = (MyObject as Form); 
     form.Invoke(new Func<object, object,object>(delegate(object _object1,object _object2) 
     { 
      returnValue = funcFunction(_object1,_object2); 
      return returnValue; 
     }), inputValue1,inputValue2); 
     return returnValue; 
    } 

    /// <summary> 
    /// Three Parameters, With ReturnValue 
    /// </summary> 
    /// <param name="funcFunction">the function needed to be delegated</param> 
    /// <param name="inputValue1">the first input parameter</param> 
    /// <param name="inputValue2">the second input parameter</param> 
    /// <param name="inputValue3">the third input parameter</param> 
    /// <returns></returns> 
    public object AsyncWithReturnValue(Func<object, object, object, object> funcFunction, object inputValue1, object inputValue2, object inputValue3) 
    { 
     object returnValue = null; 
     Form form = (MyObject as Form); 
     form.Invoke(new Func<object, object, object,object>(delegate(object _object1, object _object2,object _object3) 
     { 
      returnValue = funcFunction(_object1, _object2,_object3); 
      return returnValue; 
     }), inputValue1, inputValue2,inputValue3); 
     return returnValue; 
    } 

    /// <summary> 
    /// One Parameter,WithOut ReturnValue 
    /// </summary> 
    /// <param name="actionFunction">the function needed to be delegated</param> 
    /// <param name="inputValue">the input prameter</param> 
    public void AsyncWithOutReturnValue(Action<object> actionFunction, object inputValue) 
    { 
     Form form = (MyObject as Form); 
     form.Invoke(new Action<object>(delegate(object _object) 
     { 
      actionFunction(_object); 
     }),inputValue); 
    } 

    /// <summary> 
    /// Two Parameters,WithOut ReturnValue 
    /// </summary> 
    /// <param name="actionFunction">the function needed to be delegated</param> 
    /// <param name="inputValue1">the first input parameter</param> 
    /// <param name="inputValue2">the second input parameter</param> 
    public void AsyncWithOutReturnValue(Action<object,object> actionFunction, object inputValue1,object inputValue2) 
    { 
     Form form = (MyObject as Form); 
     form.Invoke(new Action<object,object>(delegate(object _object1,object _object2) 
     { 
      actionFunction(_object1,_object2); 
     }), inputValue1,inputValue2); 
    } 


    /// <summary> 
    /// Three Parameters, WithOut ReturnValue 
    /// </summary> 
    /// <param name="actionFunction">the function needed to be delegated</param> 
    /// <param name="inputValue1">the first input parameter</param> 
    /// <param name="inputValue2">the second input paramter</param> 
    /// <param name="inputValue3">the third input parameter</param> 
    public void AsyncWithOutReturnValue(Action<object, object,object> actionFunction, object inputValue1, object inputValue2,object inputValue3) 
    { 
     Form form = (MyObject as Form); 
     form.Invoke(new Action<object, object,object>(delegate(object _object1, object _object2,object _object3) 
     { 
      actionFunction(_object1, _object2,_object3); 
     }), inputValue1, inputValue2,inputValue3); 
    } 
} 
} 

現在我使用的類象下面這樣:

using System; 
using System.Windows.Forms; 

namespace AsyncLibAPP 
{ 
public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
     asyncLib = new AsyncLibery.AsyncLib(this); 
    } 
    AsyncLibery.AsyncLib asyncLib; 
    private void Form1_Load(object sender, EventArgs e) 
    { 
    } 
    private void test() 
    { 
     button1.Text = "test"; 
    } 
    private string test1() 
    { 
     label1.Text = "test"; 
     return "test"; 
    } 
    private string test2(object value) 
    { 
     label1.Text = value.ToString(); 
     return "test,test"; 
    } 
    private void test3(object s) 
    { 
     label1.Text = s.ToString(); 
    } 
    private void test4(object s1, object s2, object s3) 
    { 
     label1.Text = s1.ToString() + s2.ToString() + s3.ToString(); 
    } 
    private void button1_Click(object sender, EventArgs e) 
    { 
     //asyncLib.RunAsyncCrossThreads(test); 
     //string value = asyncLib.AsyncWithNoParamOneReturnValue(test1).ToString(); 
     //string value = asyncLib.Async(test2,"aaaa").ToString(); 
     // MessageBox.Show(value); 
     //asyncLib.AsyncWithOutReturnValue(test3,"sssss"); 
     asyncLib.AsyncWithOutReturnValue(test4,"aaaaaa","bbbbbbbb","cccccccc"); 
    } 
} 
} 

它運行好,但看起來這麼難看。

我計劃使用T而不是Object類型,但我不知道該怎麼做。

任何人都可以優化它嗎? thx非常。這段代碼

+2

這可能是http://codereview.stackexchange.com/ – 2012-01-06 09:22:12

+0

hoho的候選人,是的,我認爲是。 :) – CharlieShi 2012-01-06 09:36:42

回答

1

一方面這確實是非常難看的單詞「異步」。這些方法都不是異步的,它們都是同步調用,直到調用的方法結束運行纔會返回。

但最大的問題是,它僅僅是沒有必要的。你得到了lambdas在你的處置,你只需要一個方法。一個lambda可以捕獲一個變量。您的測試代碼是最好這樣寫的:

private void button1_Click(object sender, EventArgs e) { 
     string value = "aaaaaa"; 
     this.Invoke(new Action(() => test4(value, "bbbbbbbb", "cccccccc"))); 
    } 

用假變量,以避免使其過於瑣碎。並且請記住,您幾乎總是希望使用BeginInvoke(),從而在UI線程上創建工作線程塊並不具有生產力。除了需要返回值時,則需要Invoke()。這本身幾乎總是錯誤的,工人應該從他們需要的論點開始。無論你通過稍後使用Invoke()從UI組件獲得的結果都是非常隨機的,因爲工作人員與用戶的輸入沒有任何同步。

+0

嗨Passant,thx爲您的答覆。如果我需要異步運行該函數,是否需要使用BeginInvoke?有沒有其他的類可以使函數異步?我知道Action是一個委託,而Invoke()方法可以使UI元素交叉線程。另外Action不委託函數返回vlaue,如果我用returnValue運行一個函數,需要我用Func嗎?那麼如何使用它? thx非常。 :) – CharlieShi 2012-01-06 12:33:24

+0

是的。不,不是,錯誤的線程。是。 – 2012-01-06 13:09:12

相關問題