public class Tasktools {//<summary>//whether the running state is modified before executing the callback function///</summary> public bool Changerunningstatebeforecallback {get; private set;} <summary>///Whether an asynchronous task is being performed///</summary> public bool Running {get; private set;} Public Tasktools (): this (false) {}//<summary>////< /summary>//<param name= "Changerunningstatebeforecallback" > whether to modify the running state defaults before executing the callback function False</param> ; Public Tasktools (bool changerunningstatebeforecallback) {this. Changerunningstatebeforecallback = Changerunningstatebeforecallback; }///<summary>///Perform asynchronous tasks///</summary>//<typeparam name= "T" > Asynchronous task return value type & lt;/typeparam>//<param name= "Control" > Controls to invoke when manipulating UI </param>///<param name= "ASYNCFU NC > Task </param to be performed>//<param name= "callback" > callback function executed after completion of the asynchronous task </param> public void run<t> (Control contro L, func<t> Asyncfunc, action<t> callback) {if (this. Running) throw new InvalidOperationException ("The task is Running"); try {this. Running = true; task<t> task = new Task<t> (() = {try { return Asyncfunc (); } catch (Exception ex) {Console.WriteLine (ex. Message); return default (T); } }); Task. Start (); Taskcontinue<t> (Control, task, callback); } catch (Exception ex) {Console.WriteLine (ex. Message); } finally {this. Running = false; }}///<summary>///Perform asynchronous tasks///</summary>//<typeparam name= "T" > Xor Step task return value type </typeparam>//<param name= "Control" > Controls to invoke when manipulating UI </param>//<param name = "args" > Incoming parameters for Asynchronous tasks </param>//<param name= "Asyncfunc" > Task tasks to be performed </param>//<param N Ame= "Callback" > callback function executed after completion of the asynchronous task </param> public void run<t> (Control control, object args, Func<obj ECT, t> Asyncfunc, action<t> callback) {if (this. Running) throw new InvalidOperationException ("The task is Running"); try {this. Running = true; task<t> task = new task<t> ((lambdaobj) + = {try { Return Asyncfunc (lambdaobj); } catch (Exception ex) {Console.WriteLine (ex. Message); return default (T); }}, args); Task. Start (); Taskcontinue<t> (Control, task, callback); } catch (Exception ex) {Console.WriteLine (ex. Message); } finally {this. Running = false; }}///<summary>///delay to perform a task///</summary>//<param name= "Control" &G t; controls that require invoke when manipulating UI </param>///<param name= "millisecond" > number of milliseconds to execute on delay </param>//<param Name= "Callback" > callback function executed after completion of the asynchronous task </param> public void Delayedrun (int millisecond, control control, Action C Allback) {this. Run<int> (Control, () = {Thread.Sleep (millisecond);//4.0 class library return mi Llisecond; }, (Time) => {callback (); }); }///<summary>///Control.Invoke Method Package///</summary>//<typeparam name= "T "> Parameter type </typeparam>//<param name=" control "></param>//<param name=" args ">< /param>//<param name= "action" ></param> public static void Controlinvoke<t> (Control c Ontrol, T args, action<t> Action) {try {invoke<t> (control, args, Action); } catch (Exception ex) {Console.WriteLine (ex. Message); }}///<summary>//////////////<typeparam Name= "T" ></typeparam>//<param name= "control" ></param>//<param name= "task" ></p aram>//<param name= "callback" ></param> privatevoid taskcontinue<t> (Control control, task<t> task, action<t> callback) {task. ContinueWith ((lambdaaction) + = {if (this. Changerunningstatebeforecallback) {this. Running = false; } try {if (callback! = null) { There is a UI control that injects a callback function into the UI control's related thread to execute if (control! = null) { Tasktools.invoke<t> (Control, Lambdaaction.result, callback); } else {//otherwise executes the callback function within the current thread Callback (Lambdaaction.result); }}} catch (Exception ex) {CONSOLE.WR Iteline (ex. Message); } finally {this. Running = false; } }); }///<summary>///Control.Invoke Method Simple Package//Note no Try Catch///</summary> <typeparam name= "T" > Parameter types </typeparam>//<param name= "control" ></param>//< ;p Aram Name= "args" ></param>///<param Name= "action" ></param> private static void Invok E<t> (Control control, T args, action<t> Action) {//control is empty, executes the action if within the current thread (Control = = NULL) {action (args); Return }//The control is being disposed or has been disposed without executing the action if (control. Disposing | | Control. isdisposed) return; if (control. invokerequired) {control. Invoke (Action, new object[] {args}); } else {action (args); } } }
static void Main (string[] args) { tasktools task = new Tasktools (true); Task. Delayedrun (n, NULL, PNLMAINFILL2); Task. Delayedrun (+, NULL, () = { pnlmainfill (); Task. Delayedrun (+, NULL, () = //{ // Pnlmainfill (); //}); }); Console.read (); }
Asynchronous Tool Classes