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:UsersAdministratorDesktopHR-003请假单.doc"); Console.WriteLine(f); Console.ReadLine(); }