我写了一个异步类,但我不知道如何优化它

时间:2012-01-06 09:17:26

标签: c# asynchronous

我编写了一个异步类,它可以异步运行该函数,好吧,事实是这个类非常难看,见下文:

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类型,但我不知道该怎么做。

任何人都可以优化吗?非常感谢。

1 个答案:

答案 0 :(得分: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组件获得什么,都会非常随机,因为worker不会以任何方式与用户的输入同步。