ActionBlock<TInput> Sınıf

Tanım

Alınan her veri öğesi için sağlanan Action<T> bir temsilciyi çağıran bir veri akışı bloğu sağlar.

generic <typename TInput>
public ref class ActionBlock sealed : System::Threading::Tasks::Dataflow::ITargetBlock<TInput>
public sealed class ActionBlock<TInput> : System.Threading.Tasks.Dataflow.ITargetBlock<TInput>
type ActionBlock<'Input> = class
    interface ITargetBlock<'Input>
    interface IDataflowBlock
type ActionBlock<'Input> = class
    interface IDataflowBlock
    interface ITargetBlock<'Input>
Public NotInheritable Class ActionBlock(Of TInput)
Implements ITargetBlock(Of TInput)

Tür Parametreleri

TInput

Bunun ActionBlock<TInput> üzerinde çalıştığı veri türü.

Devralma
ActionBlock<TInput>
Uygulamalar

Örnekler

Aşağıdaki örnek, veri akışı bloklarını kullanarak çeşitli hesaplamalar gerçekleştirmek için sınıfının kullanımını ActionBlock<TInput> gösterir ve hesaplamaları gerçekleştirmek için gereken geçen süreyi döndürür. Bu kod örneği, Nasıl yapılır: Veri Akışı Bloğunda Paralellik Derecesini Belirtme makalesinde sağlanan daha büyük bir örneğin parçasıdır.

// Performs several computations by using dataflow and returns the elapsed
// time required to perform the computations.
static TimeSpan TimeDataflowComputations(int maxDegreeOfParallelism,
   int messageCount)
{
   // Create an ActionBlock<int> that performs some work.
   var workerBlock = new ActionBlock<int>(
      // Simulate work by suspending the current thread.
      millisecondsTimeout => Thread.Sleep(millisecondsTimeout),
      // Specify a maximum degree of parallelism.
      new ExecutionDataflowBlockOptions
      {
         MaxDegreeOfParallelism = maxDegreeOfParallelism
      });

   // Compute the time that it takes for several messages to
   // flow through the dataflow block.

   Stopwatch stopwatch = new Stopwatch();
   stopwatch.Start();

   for (int i = 0; i < messageCount; i++)
   {
      workerBlock.Post(1000);
   }
   workerBlock.Complete();

   // Wait for all messages to propagate through the network.
   workerBlock.Completion.Wait();

   // Stop the timer and return the elapsed number of milliseconds.
   stopwatch.Stop();
   return stopwatch.Elapsed;
}
' Demonstrates how to specify the maximum degree of parallelism 
' when using dataflow.
Friend Class Program
   ' Performs several computations by using dataflow and returns the elapsed
   ' time required to perform the computations.
   Private Shared Function TimeDataflowComputations(ByVal maxDegreeOfParallelism As Integer, ByVal messageCount As Integer) As TimeSpan
      ' Create an ActionBlock<int> that performs some work.
      Dim workerBlock = New ActionBlock(Of Integer)(Function(millisecondsTimeout) Pause(millisecondsTimeout), New ExecutionDataflowBlockOptions() With { .MaxDegreeOfParallelism = maxDegreeOfParallelism})
         ' Simulate work by suspending the current thread.
         ' Specify a maximum degree of parallelism.

      ' Compute the time that it takes for several messages to 
      ' flow through the dataflow block.

      Dim stopwatch As New Stopwatch()
      stopwatch.Start()

      For i As Integer = 0 To messageCount - 1
         workerBlock.Post(1000)
      Next i
      workerBlock.Complete()

      ' Wait for all messages to propagate through the network.
      workerBlock.Completion.Wait()

      ' Stop the timer and return the elapsed number of milliseconds.
      stopwatch.Stop()
      Return stopwatch.Elapsed
   End Function

   Private Shared Function Pause(ByVal obj As Object)
      Thread.Sleep(obj)
      Return Nothing
   End Function

Açıklamalar

Note

TPL Veri Akışı Kitaplığı (System.Threading.Tasks.Dataflow ad alanı) .NET ile dağıtılmaz. Visual Studio'da System.Threading.Tasks.Dataflow ad alanını yüklemek için projenizi açın, Project menüsünden NuGet Paketlerini Yönet seçin ve System.Threading.Tasks.Dataflow paketini çevrimiçi olarak arayın. Alternatif olarak, .NET Core CLI kullanarakyüklemek için dotnet add package System.Threading.Tasks.Dataflowçalıştırın.

Oluşturucular

Name Description
ActionBlock<TInput>(Action<TInput>, ExecutionDataflowBlockOptions)

Belirtilen eylem ve yapılandırma seçenekleriyle sınıfın ActionBlock<TInput> yeni bir örneğini başlatır.

ActionBlock<TInput>(Action<TInput>)

Belirtilen eylemle sınıfının yeni bir örneğini ActionBlock<TInput> başlatır.

ActionBlock<TInput>(Func<TInput,Task>, ExecutionDataflowBlockOptions)

Belirtilen eylem ve yapılandırma seçenekleriyle sınıfın ActionBlock<TInput> yeni bir örneğini başlatır.

ActionBlock<TInput>(Func<TInput,Task>)

Belirtilen eylemle sınıfının yeni bir örneğini ActionBlock<TInput> başlatır.

Özellikler

Name Description
Completion

Zaman uyumsuz işlemi ve veri akışı bloğunun tamamlanmasını temsil eden bir Task nesne alır.

InputCount

Bu blok tarafından işlenmeyi bekleyen giriş öğelerinin sayısını alır.

Yöntemler

Name Description
Complete()

Veri akışı bloğuna, daha fazla ileti kabul etmemesi veya üretmemesi ve daha fazla ertelenen ileti kullanmaması gerektiğini belirten sinyaller.

Equals(Object)

Belirtilen nesnenin geçerli nesneye eşit olup olmadığını belirler.

(Devralındığı yer: Object)
GetHashCode()

Varsayılan karma işlevi işlevi görür.

(Devralındığı yer: Object)
GetType()

Geçerli örneğin Type alır.

(Devralındığı yer: Object)
MemberwiseClone()

Geçerli Objectbasit bir kopyasını oluşturur.

(Devralındığı yer: Object)
Post(TInput)

Bir öğeyi hedef veri akışı bloğuna postalar.

ToString()

Bu IDataflowBlock örneğin biçimlendirilmiş adını temsil eden bir dize döndürür.

Belirtik Arabirim Kullanımları

Name Description
IDataflowBlock.Fault(Exception)

Veri akışı bloğunun hatalı durumda tamamlanmasına neden olur.

ITargetBlock<TInput>.OfferMessage(DataflowMessageHeader, TInput, ISourceBlock<TInput>, Boolean)

Veri akışı bloğuna bir ileti sunar ve iletiyi kullanma veya erteleme fırsatı verir.

Uzantı Metotları

Name Description
AsObserver<TInput>(ITargetBlock<TInput>)

üzerinde IObserver<T>yeni ITargetBlock<TInput> bir soyutlama oluşturur.

Post<TInput>(ITargetBlock<TInput>, TInput)

Öğesine bir öğe postalar ITargetBlock<TInput>.

SendAsync<TInput>(ITargetBlock<TInput>, TInput, CancellationToken)

Zaman uyumsuz olarak hedef ileti bloğuna bir ileti sunar ve ertelemeye olanak tanır.

SendAsync<TInput>(ITargetBlock<TInput>, TInput)

Zaman uyumsuz olarak hedef ileti bloğuna bir ileti sunar ve ertelemeye olanak tanır.

Şunlara uygulanır