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

本文关键字:我不知道 何优化 优化 一个 异步 | 更新日期: 2023-09-27 18:19:40

我写了一个异步类,它可以异步运行函数,好吧,事实上这个类很难看,如下所示:

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类型,但我不知道如何做到这一点。

有人能优化它吗?非常感谢。

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

这个代码的一个方面确实非常丑陋,那就是"异步"这个词。这些方法都不是异步的,它们都是同步调用,在被调用的方法完成运行之前不会返回。

但最大的问题是,这根本没有必要。你有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组件获得的任何内容都将是非常随机的,因为工作程序在任何方面都与用户的输入不同步。