Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions .openpublishing.redirection.json
Original file line number Diff line number Diff line change
Expand Up @@ -11502,8 +11502,7 @@
},
{
"source_path": "docs/standard/events/how-to-consume-events-in-a-web-forms-application.md",
"redirect_url": "/aspnet/web-forms/overview/how-to-consume-events",
"redirect_document_id": true
"redirect_url": "/aspnet/web-forms/overview/how-to-consume-events"
},
{
"source_path": "docs/standard/exceptions.md",
Expand Down Expand Up @@ -11610,6 +11609,11 @@
"redirect_url": "/dotnet/standard/parallel-programming/introduction-to-plinq",
"redirect_document_id": true
},
{
"source_path": "docs/standard/parallel-programming/using-tpl-with-other-asynchronous-patterns.md",
"redirect_url": "/dotnet/standard/parallel-programming/tpl-and-traditional-async-programming",
"redirect_document_id": true
},
{
"source_path": "docs/standard/portability-analyzer.md",
"redirect_url": "/dotnet/standard/analyzers/portability-analyzer",
Expand Down
33 changes: 33 additions & 0 deletions docs/core/deploying/trim-self-contained.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,39 @@ When the code is indirectly referencing an assembly through reflection, you can
</ItemGroup>
```

### Support for SSL certificates

If your app loads SSL certificates, such as in an ASP.NET Core app, you'll want to ensure that when trimming you prevent trimming assemblies that will help with loading SSL certificates.

We can update our project file to include the following for ASP.NET Core 3.1:

```xml
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>...</PropertyGroup>
<!--Include the following for .aspnetcore 3.1-->
<ItemGroup>
<TrimmerRootAssembly Include="System.Net" />
<TrimmerRootAssembly Include="System.Net.Security" />
<TrimmerRootAssembly Include="System.Security" />
</ItemGroup>
...
</Project>
```

If we're using .Net 5.0, we can update our project file to include the following:

```xml
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>...</PropertyGroup>
<!--Include the following for .net 5.0-->
<ItemGroup>
<TrimmerRootAssembly Include="System.Net.Security" />
<TrimmerRootAssembly Include="System.Security" />
</ItemGroup>
...
</Project>
```

## Trim your app - CLI

Trim your application using the [dotnet publish](../tools/dotnet-publish.md) command. When you publish your app, set the following properties:
Expand Down
5 changes: 2 additions & 3 deletions docs/fundamentals/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1734,10 +1734,9 @@ items:
href: ../standard/parallel-programming/how-to-specify-a-task-scheduler-in-a-dataflow-block.md
- name: "Walkthrough: Using BatchBlock and BatchedJoinBlock to Improve Efficiency"
href: ../standard/parallel-programming/walkthrough-using-batchblock-and-batchedjoinblock-to-improve-efficiency.md
- name: Using TPL with Other Asynchronous Patterns
href: ../standard/parallel-programming/using-tpl-with-other-asynchronous-patterns.md
- name: Use TPL with Other Asynchronous Patterns
items:
- name: TPL and Traditional .NET Framework Asynchronous Programming
- name: TPL and Traditional .NET Asynchronous Programming
href: ../standard/parallel-programming/tpl-and-traditional-async-programming.md
- name: "How to: Wrap EAP Patterns in a Task"
href: ../standard/parallel-programming/how-to-wrap-eap-patterns-in-a-task.md
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ helpviewer_keywords:
ms.assetid: bdc82f2f-4754-45a1-a81e-fe2e9c30cef9
---
# Data Structures for Parallel Programming
The .NET Framework version 4 introduces several new types that are useful in parallel programming, including a set of concurrent collection classes, lightweight synchronization primitives, and types for lazy initialization. You can use these types with any multithreaded application code, including the Task Parallel Library and PLINQ.

.NET provides several types that are useful in parallel programming, including a set of concurrent collection classes, lightweight synchronization primitives, and types for lazy initialization. You can use these types with any multithreaded application code, including the Task Parallel Library and PLINQ.

## Concurrent Collection Classes
The collection classes in the <xref:System.Collections.Concurrent?displayProperty=nameWithType> namespace provide thread-safe add and remove operations that avoid locks wherever possible and use fine-grained locking where locks are necessary. Unlike collections that were introduced in the .NET Framework versions 1.0 and 2.0, a concurrent collection class does not require user code to take any locks when it accesses items. The concurrent collection classes can significantly improve performance over types such as <xref:System.Collections.ArrayList?displayProperty=nameWithType> and <xref:System.Collections.Generic.List%601?displayProperty=nameWithType> (with user-implemented locking) in scenarios where multiple threads add and remove items from a collection.
The collection classes in the <xref:System.Collections.Concurrent?displayProperty=nameWithType> namespace provide thread-safe add and remove operations that avoid locks wherever possible and use fine-grained locking where locks are necessary. A concurrent collection class does not require user code to take any locks when it accesses items. The concurrent collection classes can significantly improve performance over types such as <xref:System.Collections.ArrayList?displayProperty=nameWithType> and <xref:System.Collections.Generic.List%601?displayProperty=nameWithType> (with user-implemented locking) in scenarios where multiple threads add and remove items from a collection.

The following table lists the new concurrent collection classes:
The following table lists the concurrent collection classes:

|Type|Description|
|----------|-----------------|
Expand All @@ -25,9 +26,9 @@ The .NET Framework version 4 introduces several new types that are useful in par
For more information, see [Thread-Safe Collections](../collections/thread-safe/index.md).

## Synchronization Primitives
The new synchronization primitives in the <xref:System.Threading?displayProperty=nameWithType> namespace enable fine-grained concurrency and faster performance by avoiding expensive locking mechanisms found in legacy multithreading code. Some of the new types, such as <xref:System.Threading.Barrier?displayProperty=nameWithType> and <xref:System.Threading.CountdownEvent?displayProperty=nameWithType> have no counterparts in earlier releases of the .NET Framework.
The synchronization primitives in the <xref:System.Threading?displayProperty=nameWithType> namespace enable fine-grained concurrency and faster performance by avoiding expensive locking mechanisms found in legacy multithreading code.

The following table lists the new synchronization types:
The following table lists the synchronization types:

|Type|Description|
|----------|-----------------|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ The second query uses the static <xref:System.IO.Directory.EnumerateDirectories%

When using <xref:System.IO.Directory.GetFiles%2A>, be sure that you have sufficient permissions on all directories in the tree. Otherwise, an exception will be thrown and no results will be returned. When using the <xref:System.IO.Directory.EnumerateDirectories%2A> in a PLINQ query, it is problematic to handle I/O exceptions in a graceful way that enables you to continue iterating. If your code must handle I/O or unauthorized access exceptions, then you should consider the approach described in [How to: Iterate File Directories with the Parallel Class](how-to-iterate-file-directories-with-the-parallel-class.md).

If I/O latency is an issue, for example with file I/O over a network, consider using one of the asynchronous I/O techniques described in [TPL and Traditional .NET Framework Asynchronous Programming](tpl-and-traditional-async-programming.md) and in this [blog post](https://devblogs.microsoft.com/pfxteam/parallel-extensions-and-io/).
If I/O latency is an issue, for example with file I/O over a network, consider using one of the asynchronous I/O techniques described in [TPL and Traditional .NET Asynchronous Programming](tpl-and-traditional-async-programming.md) and in this [blog post](https://devblogs.microsoft.com/pfxteam/parallel-extensions-and-io/).

## See also

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ In many cases, file iteration is an operation that can be easily parallelized. T
[!code-csharp[TPL_Parallel#08](../../../samples/snippets/csharp/VS_Snippets_Misc/tpl_parallel/cs/parallel_file.cs#08)]
[!code-vb[TPL_Parallel#08](../../../samples/snippets/visualbasic/VS_Snippets_Misc/tpl_parallel/vb/fileiteration08.vb#08)]

In this example, the file I/O is performed synchronously. When dealing with large files or slow network connections, it might be preferable to access the files asynchronously. You can combine asynchronous I/O techniques with parallel iteration. For more information, see [TPL and Traditional .NET Framework Asynchronous Programming](tpl-and-traditional-async-programming.md).
In this example, the file I/O is performed synchronously. When dealing with large files or slow network connections, it might be preferable to access the files asynchronously. You can combine asynchronous I/O techniques with parallel iteration. For more information, see [TPL and Traditional .NET Asynchronous Programming](tpl-and-traditional-async-programming.md).

The example uses the local `fileCount` variable to maintain a count of the total number of files processed. Because the variable might be accessed concurrently by multiple tasks, access to it is synchronized by calling the <xref:System.Threading.Interlocked.Add%2A?displayProperty=nameWithType> method.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ The following example shows how to expose an arbitrary sequence of Event-Based A

## See also

- [TPL and Traditional .NET Framework Asynchronous Programming](tpl-and-traditional-async-programming.md)
- [TPL and Traditional .NET Asynchronous Programming](tpl-and-traditional-async-programming.md)
4 changes: 2 additions & 2 deletions docs/standard/parallel-programming/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ ms.assetid: 4d83c690-ad2d-489e-a2e0-b85b898a672d

Many personal computers and workstations have multiple CPU cores that enable multiple threads to be executed simultaneously. To take advantage of the hardware, you can parallelize your code to distribute work across multiple processors.

In the past, parallelization required low-level manipulation of threads and locks. Visual Studio and the .NET Framework enhance support for parallel programming by providing a runtime, class library types, and diagnostic tools. These features, which were introduced with the .NET Framework 4, simplify parallel development. You can write efficient, fine-grained, and scalable parallel code in a natural idiom without having to work directly with threads or the thread pool.
In the past, parallelization required low-level manipulation of threads and locks. Visual Studio and .NET enhance support for parallel programming by providing a runtime, class library types, and diagnostic tools. These features, which were introduced in .NET Framework 4, simplify parallel development. You can write efficient, fine-grained, and scalable parallel code in a natural idiom without having to work directly with threads or the thread pool.

The following illustration provides a high-level overview of the parallel programming architecture in the .NET Framework:
The following illustration provides a high-level overview of the parallel programming architecture in .NET.

![.NET Parallel Programming Architecture](./media/tpl-architecture.png)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ The following illustration shows the difference between `foreach` and <xref:Syst

## Cancellation

PLINQ is integrated with the cancellation types in .NET Framework 4. (For more information, see [Cancellation in Managed Threads](../threading/cancellation-in-managed-threads.md).) Therefore, unlike sequential LINQ to Objects queries, PLINQ queries can be canceled. To create a cancelable PLINQ query, use the <xref:System.Linq.ParallelEnumerable.WithCancellation%2A> operator on the query and provide a <xref:System.Threading.CancellationToken> instance as the argument. When the <xref:System.Threading.CancellationToken.IsCancellationRequested%2A> property on the token is set to true, PLINQ will notice it, stop processing on all threads, and throw an <xref:System.OperationCanceledException>.
PLINQ is integrated with the cancellation types in .NET. (For more information, see [Cancellation in Managed Threads](../threading/cancellation-in-managed-threads.md).) Therefore, unlike sequential LINQ to Objects queries, PLINQ queries can be canceled. To create a cancelable PLINQ query, use the <xref:System.Linq.ParallelEnumerable.WithCancellation%2A> operator on the query and provide a <xref:System.Threading.CancellationToken> instance as the argument. When the <xref:System.Threading.CancellationToken.IsCancellationRequested%2A> property on the token is set to true, PLINQ will notice it, stop processing on all threads, and throw an <xref:System.OperationCanceledException>.

It is possible that a PLINQ query might continue to process some elements after the cancellation token is set.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ In many cases, <xref:System.Threading.Tasks.Parallel.For%2A?displayProperty=name
[!code-vb[TPL_Pitfalls#04](../../../samples/snippets/visualbasic/VS_Snippets_Misc/tpl_pitfalls/vb/pitfalls_vb.vb#04)]

## Limit Calls to Thread-Safe Methods
Most static methods in the .NET Framework are thread-safe and can be called from multiple threads concurrently. However, even in these cases, the synchronization involved can lead to significant slowdown in the query.
Most static methods in .NET are thread-safe and can be called from multiple threads concurrently. However, even in these cases, the synchronization involved can lead to significant slowdown in the query.

> [!NOTE]
> You can test for this yourself by inserting some calls to <xref:System.Console.WriteLine%2A> in your queries. Although this method is used in the documentation examples for demonstration purposes, do not use it in parallel loops unless necessary.
Expand All @@ -63,7 +63,7 @@ In many cases, <xref:System.Threading.Tasks.Parallel.For%2A?displayProperty=name
## Avoid Executing Parallel Loops on the UI Thread
It is important to keep your application's user interface (UI) responsive. If an operation contains enough work to warrant parallelization, then it likely should not be run that operation on the UI thread. Instead, it should offload that operation to be run on a background thread. For example, if you want to use a parallel loop to compute some data that should then be rendered into a UI control, you should consider executing the loop within a task instance rather than directly in a UI event handler. Only when the core computation has completed should you then marshal the UI update back to the UI thread.

If you do run parallel loops on the UI thread, be careful to avoid updating UI controls from within the loop. Attempting to update UI controls from within a parallel loop that is executing on the UI thread can lead to state corruption, exceptions, delayed updates, and even deadlocks, depending on how the UI update is invoked. In the following example, the parallel loop blocks the UI thread on which its executing until all iterations are complete. However, if an iteration of the loop is running on a background thread (as <xref:System.Threading.Tasks.Parallel.For%2A> may do), the call to Invoke causes a message to be submitted to the UI thread and blocks waiting for that message to be processed. Since the UI thread is blocked running the <xref:System.Threading.Tasks.Parallel.For%2A>, the message can never be processed, and the UI thread deadlocks.
If you do run parallel loops on the UI thread, be careful to avoid updating UI controls from within the loop. Attempting to update UI controls from within a parallel loop that is executing on the UI thread can lead to state corruption, exceptions, delayed updates, and even deadlocks, depending on how the UI update is invoked. In the following example, the parallel loop blocks the UI thread on which it's executing until all iterations are complete. However, if an iteration of the loop is running on a background thread (as <xref:System.Threading.Tasks.Parallel.For%2A> may do), the call to Invoke causes a message to be submitted to the UI thread and blocks waiting for that message to be processed. Since the UI thread is blocked running the <xref:System.Threading.Tasks.Parallel.For%2A>, the message can never be processed, and the UI thread deadlocks.

[!code-csharp[TPL_Pitfalls#02](../../../samples/snippets/csharp/VS_Snippets_Misc/tpl_pitfalls/cs/pitfalls.cs#02)]
[!code-vb[TPL_Pitfalls#02](../../../samples/snippets/visualbasic/VS_Snippets_Misc/tpl_pitfalls/vb/pitfalls_vb.vb#02)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ a.AsParallel().Where(...).OrderBy(...).Select(...).ForAll(x => fs.Write(x));

## Limit calls to thread-safe methods

Most static methods in the .NET Framework are thread-safe and can be called from multiple threads concurrently. However, even in these cases, the synchronization involved can lead to significant slowdown in the query.
Most static methods in .NET are thread-safe and can be called from multiple threads concurrently. However, even in these cases, the synchronization involved can lead to significant slowdown in the query.

> [!NOTE]
> You can test for this yourself by inserting some calls to <xref:System.Console.WriteLine%2A> in your queries. Although this method is used in the documentation examples for demonstration purposes, do not use it in PLINQ queries.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
// <Snippet5>
// <Snippet1>
// <Snippet1>
using System;
using System.Globalization;
using System.Runtime.Versioning;
using System.Threading;
using System.Threading.Tasks;

[assembly:TargetFramework(".NETFramework,Version=v4.6")]

public class Example
{

public static void Main()
{
decimal[] values = { 163025412.32m, 18905365.59m };
Expand Down Expand Up @@ -70,21 +65,3 @@ public static void Main()
// Formatting using the fr-FR culture on thread 1.
// 163 025 412,32 € 18 905 365,59 €
// </Snippet1>
// If the TargetFrameworkAttribute statement is removed, the example
// displays the following output:
// The example is running on thread 1
// The current culture is en-US
// Changed the current culture to fr-FR.
//
// Executing the delegate synchronously:
// Formatting using the fr-FR culture on thread 1.
// 163 025 412,32 € 18 905 365,59 €
//
// Executing a task asynchronously:
// Formatting using the en-US culture on thread 3.
// $163,025,412.32 $18,905,365.59
//
// Executing a task synchronously:
// Formatting using the fr-FR culture on thread 1.
// 163 025 412,32 € 18 905 365,59 €
// </Snippet5>
Loading