码迷,mamicode.com
首页 > 其他好文 > 详细

委托异步返回值

时间:2015-05-15 17:20:20      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:

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();
             
        }

 

委托异步返回值

标签:

原文地址:http://www.cnblogs.com/PEPE/p/4506332.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!