委託異步返回值

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)
        {
            //一、判斷文件路徑是否存在該文件
            byte[] bytes = File.ReadAllBytes(path);
            if (bytes.Length == 0)
            {
                Console.WriteLine("file not exist");
                return "file not exist";
            }
            //二、調用證據處理接口
            Console.WriteLine("一、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);
            //一、判斷文件路徑是否存在該文件
            byte[] bytes = File.ReadAllBytes(path);
            if (bytes.Length == 0)
            {
                Console.WriteLine("file not exist");
                return "file not exist";
            }
            //二、調用證據處理接口
            Console.WriteLine("一、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("二、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.一、有返回值this

 

  3.三、program代碼spa

 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();
             
        }
相關文章
相關標籤/搜索