نکات ویژه کار با عملیات نامتقارن در Blazor Server
نویسنده: علیرضا آرانی
تاریخ: ۱۴۰۱/۱۱/۲۴ ۱۶:۵۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
<p>Sync rendered by thread @IdOfRenderingThread</p>
@code
{
int IdOfRenderingThread;
protected override void OnInitialized()
{
base.OnInitialized();
IdOfRenderingThread =
System.Threading.Thread.CurrentThread.ManagedThreadId;
}
} @page "/"
<h1>Components with synchronous OnInitialized()</h1>
@for (int i = 0; i < 5; i++)
{
<SynchronousInitComponent />
} Components with synchronous OnInitialized() Sync rendered by thread 4 Sync rendered by thread 4 Sync rendered by thread 4 Sync rendered by thread 4 Sync rendered by thread 4
<p>Async rendered by thread @IdOfRenderingThread</p>
@code
{
int IdOfRenderingThread;
protected override async Task OnInitializedAsync()
{
// Runs synchronously as there is no code in base.OnInitialized(),
// so the same thread is used
await base.OnInitializedAsync().ConfigureAwait(false);
IdOfRenderingThread =
System.Threading.Thread.CurrentThread.ManagedThreadId;
// Awaiting will schedule a job for later, and we will be assigned
// whichever worker thread is next available
await Task.Delay(1000).ConfigureAwait(false);
IdOfRenderingThread =
System.Threading.Thread.CurrentThread.ManagedThreadId;
}
} @page "/async-init"
<h1>Components with asynchronous OnInitializedAsync()</h1>
@for (int i = 0; i < 5; i++)
{
<AsynchronousInitComponent/>
} Components with asynchronous OnInitializedAsync() Async rendered by thread 4 Async rendered by thread 4 Async rendered by thread 4 Async rendered by thread 4 Async rendered by thread 4
Components with asynchronous OnInitializedAsync() Async rendered by thread 7 Async rendered by thread 18 Async rendered by thread 10 Async rendered by thread 13 Async rendered by thread 11