首页 > 其他分享 >将一个普通方法改写为异步方法

将一个普通方法改写为异步方法

时间:2023-03-17 11:56:22浏览次数:52  
标签:异步 textBox1 NewLine 改写 callBack 方法 Environment AppendText

如何将一个普通方法改写成异步方法?

 

/// <summary>
        /// 把一个普通无参,无返回值的方法转为异步方法
        /// </summary>
        /// <param name="srcAction">要转的方法</param>
        /// <param name="callBack">回调方法</param>
        /// <returns></returns>
        public static async Task ToAsync(Action srcAction, Action callBack)
        {
            Func<Task> t = () =>
            {
                return Task.Run(() => srcAction());
            };
            await t();
            if (callBack != null)
            {
                callBack();
            }
        }

        /// <summary>
        /// 把一个无参,有返回值的方法转为异步
        /// </summary>
        /// <typeparam name="T">泛型类型</typeparam>
        /// <param name="srcAction">被转的源方法</param>
        /// <param name="callBack">回调函数</param>
        /// <returns></returns>
        public static async Task ToAsync<T>(Func<T> srcAction, Action<T> callBack)
        {
            Func<Task<T>> t = () =>
            {
                return Task<T>.Run(() => srcAction());
            };
            var resualt = await t();

            if (callBack != null)
            {
                callBack(resualt);
            }
        }

 

 整体实现代码如下:

using System;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 普通方法转异步方法
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //无参无返回
        private void button1_Click(object sender, EventArgs e)
        {
            this.textBox1.AppendText("开始执行任务1" + Environment.NewLine);
            ToAsync(new Action(() =>
            {
                Thread.Sleep(3000);
                this.Invoke(new Action(() =>
                {
                    this.textBox1.AppendText("完成任务1延时3s" + Environment.NewLine);
                }));

            }), () =>
            {
                this.textBox1.AppendText("完任务1" + Environment.NewLine);
            });
        }
        //无参有返回
        private void button2_Click(object sender, EventArgs e)
        {
            this.textBox1.AppendText("开始执行任务2" + Environment.NewLine);
            ToAsync<string>(() =>
            {
                Thread.Sleep(3000);
                return "完成任务2延时3s";
            }, (s) =>
            {
                this.textBox1.AppendText(s + Environment.NewLine);
            });
            this.textBox1.AppendText("完任务2" + Environment.NewLine);
        }
        //同步方法
        private void button3_Click(object sender, EventArgs e)
        {
            this.textBox1.AppendText("开始执行任务0" + Environment.NewLine);
            Thread.Sleep(3000);
            this.textBox1.AppendText("完成任务0的延时3s" + Environment.NewLine);
            this.textBox1.AppendText("完任务0" + Environment.NewLine);
        }


        /// <summary>
        /// 把一个普通无参,无返回值的方法转为异步方法
        /// </summary>
        /// <param name="srcAction">要转的方法</param>
        /// <param name="callBack">回调方法</param>
        /// <returns></returns>
        public static async Task ToAsync(Action srcAction, Action callBack)
        {
            Func<Task> t = () =>
            {
                return Task.Run(() => srcAction());
            };
            await t();
            if (callBack != null)
            {
                callBack();
            }
        }

        /// <summary>
        /// 把一个无参,有返回值的方法转为异步
        /// </summary>
        /// <typeparam name="T">泛型类型</typeparam>
        /// <param name="srcAction">被转的源方法</param>
        /// <param name="callBack">回调函数</param>
        /// <returns></returns>
        public static async Task ToAsync<T>(Func<T> srcAction, Action<T> callBack)
        {
            Func<Task<T>> t = () =>
            {
                return Task<T>.Run(() => srcAction());
            };
            var resualt = await t();

            if (callBack != null)
            {
                callBack(resualt);
            }
        }
    }
}

 

标签:异步,textBox1,NewLine,改写,callBack,方法,Environment,AppendText
From: https://www.cnblogs.com/hanzq/p/17226162.html

相关文章