Created
January 1, 2021 14:47
-
-
Save ierhalim/3ae2b56ffbcc4bba43ad87c28323aed0 to your computer and use it in GitHub Desktop.
Y8O1 Paralel programlama
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using Microsoft.AspNetCore.Mvc; | |
| using System.Collections.Generic; | |
| using System.Diagnostics; | |
| using System.Linq; | |
| using System.Threading; | |
| using System.Threading.Tasks; | |
| namespace AsyncAwaitExample.Controllers | |
| { | |
| public class ExampleController : ControllerBase | |
| { | |
| [HttpGet("/api/items")] | |
| public async Task<IActionResult> GetItems() | |
| { | |
| Debug.Print("Request handled at:{0}", Thread.CurrentThread.ManagedThreadId); | |
| var items = await LoadItemsAsnyc(); | |
| Debug.Print("After await, current thread id:{0}", Thread.CurrentThread.ManagedThreadId); | |
| return Ok(items); | |
| } | |
| [NonAction] | |
| private Task<List<string>> LoadItemsAsnyc() | |
| { | |
| Debug.Print("LoadItemsAsnyc method runnint at:{0}", Thread.CurrentThread.ManagedThreadId); | |
| var itemsAsInteger = Enumerable.Range(1, 200); | |
| // async api kullanımına örnek sağlamak amacı ile geliştirildi. | |
| // Microsoft dökümanlarında Task.Run methodunu çalıştırıp hemen arkasından await keyi kullanmak yapılmaması gereken işlerde ilk sıradadır. | |
| return Task.Run<List<string>>(() => | |
| { | |
| return itemsAsInteger.Select(item => $"Item-{item}").ToList(); | |
| }); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment