Task Usage tasks waiting for wait instance

Source: Internet
Author: User
1.Wait Usage

By default, a task is executed by an asynchronous thread in a wired pool, whether it is done or not, and can be judged by the property iscompleted of the task.

If you want to work on a child thread after the work is done, the subsequent main thread can be worked on by calling task. Wait () waits for the thread to finish, and after the wait is called, the current thread is blocked until the child thread finishes.

code example:

static void Main (string[] args)        {            Task t = Task.run (() =              {                  thread.sleep ();                  Console.WriteLine ("Lance");                  Thread.Sleep (+);              });            Console.WriteLine ("t.iscompleted=" + t.iscompleted);            T.wait ();            Console.WriteLine ("t.iscompleted=" + t.iscompleted);        }

Operation Result:

 

2.Wait Set Wait Time

static void Main (string[] args)        {            Task t = Task.run (() =              {                  thread.sleep ();                  Console.WriteLine ("Lance");                  Thread.Sleep (+);              });            Console.WriteLine ("t.iscompleted=" + t.iscompleted);            BOOL iscomplate= t.wait (+);            Console.WriteLine ("Wait 200 milliseconds after t.iscompleted=" + t.iscompleted);            Thread.Sleep (+);            Console.WriteLine ("t.iscompleted=" + t.iscompleted);        }

Operation Result:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.