CountdownEvent Kelas
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Mewakili primitif sinkronisasi yang disinyalir ketika jumlahnya mencapai nol.
public ref class CountdownEvent : IDisposable
public class CountdownEvent : IDisposable
[System.Runtime.InteropServices.ComVisible(false)]
public class CountdownEvent : IDisposable
type CountdownEvent = class
interface IDisposable
[<System.Runtime.InteropServices.ComVisible(false)>]
type CountdownEvent = class
interface IDisposable
Public Class CountdownEvent
Implements IDisposable
- Warisan
-
CountdownEvent
- Atribut
- Penerapan
Contoh
Contoh berikut menunjukkan cara menggunakan CountdownEvent:
using System;
using System.Collections.Concurrent;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
class Example
{
static async Task Main()
{
// Initialize a queue and a CountdownEvent
ConcurrentQueue<int> queue = new ConcurrentQueue<int>(Enumerable.Range(0, 10000));
CountdownEvent cde = new CountdownEvent(10000); // initial count = 10000
// This is the logic for all queue consumers
Action consumer = () =>
{
int local;
// decrement CDE count once for each element consumed from queue
while (queue.TryDequeue(out local)) cde.Signal();
};
// Now empty the queue with a couple of asynchronous tasks
Task t1 = Task.Factory.StartNew(consumer);
Task t2 = Task.Factory.StartNew(consumer);
// And wait for queue to empty by waiting on cde
cde.Wait(); // will return when cde count reaches 0
Console.WriteLine("Done emptying queue. InitialCount={0}, CurrentCount={1}, IsSet={2}",
cde.InitialCount, cde.CurrentCount, cde.IsSet);
// Proper form is to wait for the tasks to complete, even if you know that their work
// is done already.
await Task.WhenAll(t1, t2);
// Resetting will cause the CountdownEvent to un-set, and resets InitialCount/CurrentCount
// to the specified value
cde.Reset(10);
// AddCount will affect the CurrentCount, but not the InitialCount
cde.AddCount(2);
Console.WriteLine("After Reset(10), AddCount(2): InitialCount={0}, CurrentCount={1}, IsSet={2}",
cde.InitialCount, cde.CurrentCount, cde.IsSet);
// Now try waiting with cancellation
CancellationTokenSource cts = new CancellationTokenSource();
cts.Cancel(); // cancels the CancellationTokenSource
try
{
cde.Wait(cts.Token);
}
catch (OperationCanceledException)
{
Console.WriteLine("cde.Wait(preCanceledToken) threw OCE, as expected");
}
finally
{
cts.Dispose();
}
// It's good to release a CountdownEvent when you're done with it.
cde.Dispose();
}
}
// The example displays the following output:
// Done emptying queue. InitialCount=10000, CurrentCount=0, IsSet=True
// After Reset(10), AddCount(2): InitialCount=10, CurrentCount=12, IsSet=False
// cde.Wait(preCanceledToken) threw OCE, as expected
Imports System.Collections.Concurrent
Imports System.Linq
Imports System.Threading
Imports System.Threading.Tasks
Module Example
Sub Main()
' Initialize a queue and a CountdownEvent
Dim queue As New ConcurrentQueue(Of Integer)(Enumerable.Range(0, 10000))
Dim cde As New CountdownEvent(10000)
' initial count = 10000
' This is the logic for all queue consumers
Dim consumer As Action =
Sub()
Dim local As Integer
' decrement CDE count once for each element consumed from queue
While queue.TryDequeue(local)
cde.Signal()
End While
End Sub
' Now empty the queue with a couple of asynchronous tasks
Dim t1 As Task = Task.Factory.StartNew(consumer)
Dim t2 As Task = Task.Factory.StartNew(consumer)
' And wait for queue to empty by waiting on cde
cde.Wait()
' will return when cde count reaches 0
Console.WriteLine("Done emptying queue. InitialCount={0}, CurrentCount={1}, IsSet={2}", cde.InitialCount, cde.CurrentCount, cde.IsSet)
' Proper form is to wait for the tasks to complete, even if you know that their work
' is done already.
Task.WaitAll(t1, t2)
' Resetting will cause the CountdownEvent to un-set, and resets InitialCount/CurrentCount
' to the specified value
cde.Reset(10)
' AddCount will affect the CurrentCount, but not the InitialCount
cde.AddCount(2)
Console.WriteLine("After Reset(10), AddCount(2): InitialCount={0}, CurrentCount={1}, IsSet={2}", cde.InitialCount, cde.CurrentCount, cde.IsSet)
' Now try waiting with cancellation
Dim cts As New CancellationTokenSource()
cts.Cancel()
' cancels the CancellationTokenSource
Try
cde.Wait(cts.Token)
Catch generatedExceptionName As OperationCanceledException
Console.WriteLine("cde.Wait(preCanceledToken) threw OCE, as expected")
Finally
cts.Dispose()
End Try
' It's good to release a CountdownEvent when you're done with it.
cde.Dispose()
End Sub
End Module
' The example displays the following output:
' Done emptying queue. InitialCount=10000, CurrentCount=0, IsSet=True
' After Reset(10), AddCount(2): InitialCount=10, CurrentCount=12, IsSet=False
' cde.Wait(preCanceledToken) threw OCE, as expected
Konstruktor
| Nama | Deskripsi |
|---|---|
| CountdownEvent(Int32) |
Menginisialisasi instans CountdownEvent kelas baru dengan jumlah yang ditentukan. |
Properti
| Nama | Deskripsi |
|---|---|
| CurrentCount |
Mendapatkan jumlah sinyal yang tersisa yang diperlukan untuk mengatur peristiwa. |
| InitialCount |
Mendapatkan jumlah sinyal yang awalnya diperlukan untuk mengatur peristiwa. |
| IsSet |
Menunjukkan apakah CountdownEvent jumlah objek saat ini telah mencapai nol. |
| WaitHandle |
WaitHandle Mendapatkan yang digunakan untuk menunggu peristiwa diatur. |
Metode
| Nama | Deskripsi |
|---|---|
| AddCount() |
Menaikkan CountdownEventjumlah saat ini satu per satu. |
| AddCount(Int32) |
Menaikkan CountdownEventjumlah saat ini menurut nilai tertentu. |
| Dispose() |
Merilis semua sumber daya yang digunakan oleh instans CountdownEvent kelas saat ini. |
| Dispose(Boolean) |
Merilis sumber daya yang tidak dikelola yang digunakan oleh CountdownEvent, dan secara opsional merilis sumber daya terkelola. |
| Equals(Object) |
Menentukan apakah objek yang ditentukan sama dengan objek saat ini. (Diperoleh dari Object) |
| GetHashCode() |
Berfungsi sebagai fungsi hash default. (Diperoleh dari Object) |
| GetType() |
Mendapatkan Type instans saat ini. (Diperoleh dari Object) |
| MemberwiseClone() |
Membuat salinan dangkal dari Objectsaat ini. (Diperoleh dari Object) |
| Reset() |
Mereset CurrentCount ke nilai InitialCount. |
| Reset(Int32) |
Mereset properti ke InitialCount nilai tertentu. |
| Signal() |
Mendaftarkan sinyal dengan CountdownEvent, mengurangi nilai CurrentCount. |
| Signal(Int32) |
Mendaftarkan beberapa sinyal dengan CountdownEvent, mengurangi nilai dengan jumlah CurrentCount yang ditentukan. |
| ToString() |
Mengembalikan string yang mewakili objek saat ini. (Diperoleh dari Object) |
| TryAddCount() |
Upaya untuk bertambah CurrentCount satu per satu. |
| TryAddCount(Int32) |
Mencoba untuk menaikkan CurrentCount dengan nilai tertentu. |
| Wait() |
Memblokir utas saat ini hingga CountdownEvent ditetapkan. |
| Wait(CancellationToken) |
Memblokir utas saat ini hingga CountdownEvent diatur, sambil mengamati CancellationToken. |
| Wait(Int32, CancellationToken) |
Memblokir utas saat ini hingga CountdownEvent diatur, menggunakan bilangan bulat bertanda tangan 32-bit untuk mengukur batas waktu, sambil mengamati CancellationToken. |
| Wait(Int32) |
Memblokir utas saat ini hingga CountdownEvent diatur, menggunakan bilangan bulat bertanda 32-bit untuk mengukur batas waktu. |
| Wait(TimeSpan, CancellationToken) |
Memblokir utas saat ini hingga CountdownEvent diatur, menggunakan TimeSpan untuk mengukur batas waktu, sambil mengamati CancellationToken. |
| Wait(TimeSpan) |
Memblokir utas saat ini hingga CountdownEvent diatur, menggunakan TimeSpan untuk mengukur batas waktu. |
Berlaku untuk
Keamanan Thread
Semua anggota CountdownEvent publik dan terlindungi aman utas dan dapat digunakan secara bersamaan dari beberapa utas, dengan pengecualian Dispose(), yang hanya boleh digunakan ketika semua operasi lain pada CountdownEvent telah selesai, dan Reset(), yang seharusnya hanya digunakan ketika tidak ada utas lain yang mengakses peristiwa.