我寫它可以異步運行的功能,那麼,事實是該類非常難看異步類,見下圖:我寫了一個異步類,但我不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非常。這段代碼
這可能是http://codereview.stackexchange.com/ – 2012-01-06 09:22:12
hoho的候選人,是的,我認爲是。 :) – CharlieShi 2012-01-06 09:36:42