The task has a return value example:
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Threading.Tasks;6 7 8 namespaceDemo9 {Ten class Program One { A - Static voidMain (string[] args) - { the -system.threading.tasks.task<string> task = task.factory.startnew<string>(Test); - -Console.WriteLine ("Result:"+task. Result); +Console.WriteLine ("Status:"+task. Status); -Console.WriteLine ("isfaulted:"+task. isfaulted); +Console.WriteLine ("iscompleted:"+task. iscompleted); AConsole.WriteLine ("IsCanceled:"+task. IsCanceled); at Console.readkey (); - } - - Public Static stringTest () - { -Console.WriteLine ("Call the test method:"+"DDDDFDFD"); in return "call the result returned by the Async method"; - } to + } -}
Is the result of running the above code:
Tasks that do not return a value:
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Threading.Tasks;6 7 8 namespaceDemo9 {Ten class Program One { A - Static voidMain (string[] args) - { the -System.Threading.Tasks.Task Task =Task.Factory.StartNew (Test); - -Console.WriteLine ("Status:"+task. Status); +Console.WriteLine ("isfaulted:"+task. isfaulted); -Console.WriteLine ("iscompleted:"+task. iscompleted); +Console.WriteLine ("IsCanceled:"+task. IsCanceled); A Console.readkey (); at } - - Public Static voidTest () - { -Console.WriteLine ("Call the test method:"+"DDDDFDFD"); - } in - } to}
The following is the result of running the above code:
(Task) Task asynchronous (TAP) usage