委托异步返回值

1:如何新起线程

  新起一个线程的方法,可以使用Thread,BackgroundWorker ,ThreadPool,控件.BeginInvoke,委托.BeginInvoke,Timer。

 

2:异步调用返回值

上码:

复制代码
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace AsyTest
{
    internal delegate string ArchiveEventHandle(string sender, string path);

     
    public class AsyncMethod
    {
        /// <summary>
        /// 异步返回值
        /// </summary>
        private string returnFlag = string.Empty;

        /// <summary>
        /// 文档函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="path"></param>
        /// <returns></returns>
        public string ArchiveFoo(string sender, string path)
        {
            //1、判断文件路径是否存在该文件
            byte[] bytes = File.ReadAllBytes(path);
            if (bytes.Length == 0)
            {
                Console.WriteLine("file not exist");
                return "file not exist";
            }
            //2、调用证据处理接口
            Console.WriteLine("1、file  exist");
            returnFlag = "返回值:file exist";
            return "file exist";
        }

        /// <summary>
        /// 文档函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="path"></param>
        /// <returns></returns>
        public string ArchiveFoo2(string sender, string path)
        {
            //Thread.Sleep(3000);
            //1、判断文件路径是否存在该文件
            byte[] bytes = File.ReadAllBytes(path);
            if (bytes.Length == 0)
            {
                Console.WriteLine("file not exist");
                return "file not exist";
            }
            //2、调用证据处理接口
            Console.WriteLine("1、file  exist");
            returnFlag = "返回值:file exist";
            return "file exist";
        }


        /// <summary>
        /// 回调函数
        /// </summary>
        /// <param name="ar"></param>
        public void ArchiveCallBack(IAsyncResult ar)
        {
            CallBack();
            ArchiveEventHandle archiveEventHandle = (ArchiveEventHandle)ar.AsyncState;
            string rel = archiveEventHandle.EndInvoke(ar);
            Console.WriteLine("EndInvoke:" + ar.IsCompleted);
            //证据平台函数
        }


        /// <summary>
        /// 回调函数
        /// </summary>
        /// <returns></returns>
        public string CallBack()
        {
            Console.WriteLine("2、callBack");
            return "";
        }


        /// <summary>
        /// 异步函数-无返回值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="path"></param>
        public string ArchiveAsync(string sender, string path)
        {
            ArchiveEventHandle archiveEventHandle = new ArchiveEventHandle(ArchiveFoo);
            AsyncCallback callback = new AsyncCallback(this.ArchiveCallBack);
            archiveEventHandle.BeginInvoke(sender, path, callback, archiveEventHandle);
            return returnFlag;
        }



        /// <summary>
        /// 异步函数2-返回值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="path"></param>
        public string ArchiveAsync2(string sender, string path)
        {
            ArchiveEventHandle archiveEventHandle = new ArchiveEventHandle(ArchiveFoo2);
            AsyncCallback callback = new AsyncCallback(this.ArchiveCallBack);
            IAsyncResult result = archiveEventHandle.BeginInvoke(sender, path, callback, archiveEventHandle);
            WaitHandle waitHandle = result.AsyncWaitHandle;
            waitHandle.WaitOne();
            //Console.WriteLine(result.IsCompleted);//确定异步调用何时完成
            return returnFlag;
        }
    }
}
复制代码
 

3、测试结果

  3.1、无返回值

   

  3.1、有返回值

 

  3.3、program代码

复制代码
 static void Main(string[] args)
        {
            
            AsyncMethod asy=new AsyncMethod();
            string f =asy.ArchiveAsync2("abc@sina.com", @"C:\Users\Administrator\Desktop\HR-003请假单.doc");
            Console.WriteLine(f);
            Console.ReadLine();
             
        }
复制代码

 

posted @   PEPE YU  阅读(635)  评论(1编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· SQL Server 2025 AI相关能力初探
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
点击右上角即可分享
微信分享提示