Thread<T>
Struct · namespace System · Since 0.02
struct Thread<T>The handle of a running thread function that returns a T: start Square(6) runs thread int Square(int x) on
its own OS thread and returns a Thread<int> right away.
thread int Square(int x) { return x * x; }
Thread<int> t = start Square(6);Console.WriteLine(t.Join()); // 36A thread void function returns the non-generic Thread, which has the same methods except that Join returns
nothing. See the language guide (docs/language/threading.md) for what a thread function may see.
Methods
Section titled “Methods”| Name | Description |
|---|---|
Join |
Waits until the thread has finished and returns what it returned - also if it was cancelled (a cancelled thread still returns a value of type T). |
Cancel |
Requests cancellation and does not wait: the thread notices it by reading Thread.Cancelled and decides itself when to stop. |
CancelAndWait |
Requests cancellation and waits until the thread has finished. |
IsCompleted |
Whether the thread has finished: by returning, or after a cancellation request. |
IsCancelled |
Whether Thread<T>.Cancel or Thread<T>.CancelAndWait was called; the thread may still be running. |
T Join()Waits until the thread has finished and returns what it returned - also if it was cancelled (a cancelled thread
still returns a value of type T).
Cancel
Section titled “Cancel”void Cancel()Requests cancellation and does not wait: the thread notices it by reading Thread.Cancelled and decides itself
when to stop.
CancelAndWait
Section titled “CancelAndWait”void CancelAndWait()Requests cancellation and waits until the thread has finished.
IsCompleted
Section titled “IsCompleted”bool IsCompleted()Whether the thread has finished: by returning, or after a cancellation request.
IsCancelled
Section titled “IsCancelled”bool IsCancelled()Whether Thread<T>.Cancel or Thread<T>.CancelAndWait was called; the thread may still be running.