05 - How To - Unlink Dataflow Blocks

You might also like

Download as pdf
Download as pdf
You are on page 1of 2
sano? How to: Unling Dstatow Blocks How to: Unlink Dataflow Blocks .NET Framework (current version) ‘This document describes how to unlink a target dataflow block from its source. VTip The TPL Dataflow Library (System. Threading Tasks Dataflow namespace) isnot distributed with the NET Framework 45. To install the System Threading Tasks Dataflow namespace, open your project in Visual Studio 2012, choose Manage NuGet Packages from the Project menu, and search online for the Microsoft. Tp1 .Dataflow package. Example ‘The following example creates three TransformBlock abjects, each of which calls the TrySolution method to compute a value. This example requires only the result from the first call to TrySolution to finish, ve Inports System Inports System. Threading Inports System.Threading. Tasks.Dataflow * Demonstrates how to unlink dataflow blocks Friend Class DataflowReceivesny "Receives the value fron the First provided source that has Public Shared Function ReceiveFromAny(Of T)(ParamArray ByVal sources() As ISourceBlock(Of D) ast "create a WeiteOnceBlock object and Link it to each source block. bin writeonceslock = New lrsteonceBlock(OF T)(Function(e) e) For Each source In sources "setting MaxMessages to one instructs " the source block to unlink from the kniteOnceBlock object after offering the WriteonceBlock object one message. source. LinkTo(writeOnceBlock, New DataflowLinkOptions with {.MaxMessages = 1}) Next source Return the first value that is offered to the WritednceBlock abject. Return writeOnceBlock.Receive() nd Function " Denonstrates a function that takes several seconds to produce a result. Private Shared Function TrySolution(ByVal n As Integer, ByVal ct As CancellationToken) As Integer * Simulate a lengthy operation that completes within three seconds or when the provided CancellationToken object is cancelled. Spindiait.Spinuntil(Function() ct.TsCancellationRequested, New Random() .Next (3800) ) "Return a value. Return n + 42 End Function Shared Sub Main(ayval args() As String) " Create a shared CancellationTokenSource object to enable the * TrySolution method to be cancelled. Dim cts = New CancellationTokenSource() " Create three TransformBlock objects. "Each TransformBlock object calls the TrySolution method. Dim action As Func(OF Integer, Integer) = Function(n) TrySolution(n, cts.Token) Dim trySolution1 = New TransformBlock(Of Integer, Integer)(action) Dim trySolution2 = New TransformBlock(Of Integer, Integer)(action) Dim trySolution3 = New TransformBlock(Of Integer, Integer)(action) hpssimsch microsoft comen-usibrarynh228600{deprirar, v=vs. 110). aspx 12 sano? How to: Unling Dstatow Blocks * Post data to each TransformBlockcint, int> object. trySolution.Post (11) ‘trySolution2.Post (21) ‘trySolution3.Post (31) * call the ReceiveFromAny method to receive the result from the * first TransformBlockcint, int> object to finish. Dim result As Integer = ReceiveFromAny(trySolution1, trySolution2, trySolution3) * Cancel all calls to TrySolution that are still active. cts.Cancel() * Print the result to the console. Console.WriteLine("The solution is {@).", result) cts.Dispose() End Sub End Class * sample output: “The solution is 53. To receive the value from the first TransformBlock object that finishes, this example defines the ReceiveFromAny(T) method. The ReceiveFromAny(T) method accepts an array of ISourceBlock objects and links each of these objects to a WriteOnceBlock object. When you use the LinkTo method to link a source dataflow block to a target block, the source propagates messages to the target as data becomes available. Because the WriteOnceBlock class accepts only the first message that it is offered, the ReceiveFroAny(T) method produces its result by calling the Receive object. The LinkTo method has an overloaded version that takes a Boolean parameter, unLinkAfterOne that, when its set to True, instructs the source block to unlink from the target after the target receives one message from the source. Itis important for the WriteOnceBlack object to unlink from its sources because the relationship between the array of sources and the WriteOnceBlack object is no longer required after the WriteOnceBlock object receives a message. To enable the remaining calls to TrySolution to end after one of them computes a value, the TrySolution method takes a CancellationToken object t the call to ReceiveFromAny(T) returns. The SpinUntll method returns when t CancellationToken object is canceled. at is canceled Compiling the Code Copy the example code and paste it in a Visual Studio project, or paste it in a file that is named DataflowReceiveany..cs (DataflowReceiveAny vb for Visual Basic), and then run the following command in a Visual Studio Command Prompt window. Visual C# csc.exe /r:System. Threading. rasks,Dataflow.dll DataflowReceiveAny.cs Visual Basic vbc.exe /r:System.Threading.Tasks.Dataflow.dll DataflowReceiveAny.vb Robust Programming See Also Dataflow © 2017 Microsoft hnpssimsch microsoft comen-usibraryhn228600{deprirtr,v=vs. 110) aspx 2

You might also like