Skip to content

Commit 351c342

Browse files
authored
Blazor async guidance (#18958)
1 parent 8c9e620 commit 351c342

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

aspnetcore/blazor/components/index.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ description: Learn how to create and use Razor components, including how to bind
55
monikerRange: '>= aspnetcore-3.1'
66
ms.author: riande
77
ms.custom: mvc
8-
ms.date: 06/11/2020
8+
ms.date: 06/25/2020
99
no-loc: [Blazor, "Identity", "Let's Encrypt", Razor, SignalR]
1010
uid: blazor/components/index
1111
---
@@ -416,12 +416,25 @@ While capturing component references use a similar syntax to [capturing element
416416
> [!NOTE]
417417
> Do **not** use component references to mutate the state of child components. Instead, use normal declarative parameters to pass data to child components. Use of normal declarative parameters result in child components that rerender at the correct times automatically.
418418
419-
## Invoke component methods externally to update state
419+
## Synchronization context
420420

421421
Blazor uses a synchronization context (<xref:System.Threading.SynchronizationContext>) to enforce a single logical thread of execution. A component's [lifecycle methods](xref:blazor/components/lifecycle) and any event callbacks that are raised by Blazor are executed on the synchronization context.
422422

423423
Blazor Server's synchronization context attempts to emulate a single-threaded environment so that it closely matches the WebAssembly model in the browser, which is single threaded. At any given point in time, work is performed on exactly one thread, giving the impression of a single logical thread. No two operations execute concurrently.
424424

425+
### Avoid thread-blocking calls
426+
427+
Generally, don't call the following methods. The following methods block the thread and thus block the app from resuming work until the underlying <xref:System.Threading.Tasks.Task> is complete:
428+
429+
* <xref:System.Threading.Tasks.Task%601.Result%2A>
430+
* <xref:System.Threading.Tasks.Task.Wait%2A>
431+
* <xref:System.Threading.Tasks.Task.WaitAny%2A>
432+
* <xref:System.Threading.Tasks.Task.WaitAll%2A>
433+
* <xref:System.Threading.Thread.Sleep%2A>
434+
* <xref:System.Runtime.CompilerServices.TaskAwaiter.GetResult%2A>
435+
436+
### Invoke component methods externally to update state
437+
425438
In the event a component must be updated based on an external event, such as a timer or other notifications, use the `InvokeAsync` method, which dispatches to Blazor's synchronization context. For example, consider a *notifier service* that can notify any listening component of the updated state:
426439

427440
```csharp

0 commit comments

Comments
 (0)