namespace AsyncDelegateTest { /// <summary> /// HTTP 回应 /// </summary> public class HTTPResponse { /// <summary> /// 回应错误码 /// </summary> public int errorCode; /// <summary> /// 回应消息串 /// </summary> public string response; } /// <summary> /// 示例程序 /// </summary> public class HTTPRequest { /// <summary> /// 请求成功 委托定义 /// </summary> /// <param name="originalRequest"></param> /// <param name="response"></param> public delegate void OnRequestFinishedDelegate(HTTPRequest originalRequest, HTTPResponse response); /// <summary> /// 下载进度委托示例 /// </summary> /// <param name="originalRequest"></param> /// <param name="response"></param> public delegate void OnDownloadProgressDelegate(HTTPRequest originalRequest, long downloaded, long downloadLength); /// <summary> /// 上传进度委托示例 /// </summary> /// <param name="originalRequest"></param> /// <param name="uploaded"></param> /// <param name="uploadLength"></param> public delegate void OnUploadProgressDelegate(HTTPRequest originalRequest, long uploaded, long uploadLength);
/// <summary> /// 异步回调方法 /// </summary> public OnRequestFinishedDelegate onRequestFinished; public OnDownloadProgressDelegate onDownloadProgress; public OnUploadProgressDelegate onUploadProgress;
public string httptype; public string url; }
public static class HTTP { /// <summary> /// 测试代码 /// </summary> /// <param name="httpRequest"></param> public static void Send(this HTTPRequest httpRequest) { ThreadStart reqestSimulate = () => { int length = 10; if (httpRequest.onUploadProgress != null) { for (int i = 0; i < length; i++) { Thread.Sleep(50); httpRequest.onUploadProgress.Invoke(httpRequest, i, length); } } if (httpRequest.onDownloadProgress != null) { for (int i = 0; i < length; i++) { Thread.Sleep(50); httpRequest.onDownloadProgress.Invoke(httpRequest, i, length); } } if (httpRequest.onRequestFinished != null) { HTTPResponse response = new HTTPResponse(); response.response = httpRequest.url + ":responsed!"; response.errorCode = 200; httpRequest.onRequestFinished.Invoke(httpRequest, response); } };
Thread thread = new Thread(reqestSimulate); thread.Start(); }
using System; using System.Collections.Generic; using System.Text; using System.Threading; using System.Threading.Tasks;
namespace AsyncTaskTest { /// <summary> /// 测试不带泛型参数的Task /// </summary> public class TaskTest_NonGeneric { static void TaskMethod(string taskname) { Console.WriteLine("Task {0} is running on a thread id {1}. Is thread pool thread: {2}", taskname, Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.IsThreadPoolThread); Thread.Sleep(500); }
public static void Test() { Task task1 = new Task(() => TaskMethod("T1")); //TaskMethod 具体任务操作 task1.Start(); Task.WaitAll(task1);//等待任务结束
using System; using System.Collections.Generic; using System.Text; using System.Threading; using System.Threading.Tasks;
namespace AsyncTaskTest { public class TaskTest { static int TaskMethod(string taskname) { Console.WriteLine("Task {0} is running on a thread id {1}. Is thread pool thread: {2}", taskname, Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.IsThreadPoolThread); Thread.Sleep(500); return 100; } static Task<int> CreateTask(string name) { return new Task<int>(() => TaskMethod(name)); }
public static void Test() { TaskMethod("Main Thread Task");
Task<int> task = CreateTask("T1"); task.Start(); int result = task.Result; Console.WriteLine("T1 Result is: {0}", result);
task = CreateTask("T2"); //该任务会运行在主线程中 task.RunSynchronously(); result = task.Result; Console.WriteLine("T2 Result is: {0}", result);
/// <summary> /// 任务调度器测试 /// </summary> class TaskSchedulerTest { static void TaskMethod(string taskname) { Console.WriteLine("Task {0} is running on a thread id {1}. Is thread pool thread: {2}", taskname, Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.IsThreadPoolThread); Thread.Sleep(500); } static Task CreateTask(string name) { return new Task(() => TaskMethod(name)); }
public static void Test() { Console.WriteLine(quot;Main, ThreadID: {Thread.CurrentThread.ManagedThreadId}"); for (int i = 0; i < 10; i++) { var t = CreateTask("T" + (i+1)); t.Start(MyTaskScheduler.Current); } } } }
using System; using System.Threading; using System.Threading.Tasks;
namespace AsyncTest { public class AsyncTest_NonGeneric { private static readonly Random rand = new Random(); static void TaskMethod(string taskname) { Console.WriteLine("Task {0} is running on a thread id {1}. Is thread pool thread: {2}", taskname, Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.IsThreadPoolThread); Thread.Sleep(rand.Next(100,500)); Console.WriteLine("Task {0} Completed", taskname); }