async-await

Do I need to synchronize resource access between Tasks with the default TaskScheduler?

允我心安 提交于 2021-02-05 07:58:06
问题 I'm programming with Tasks and await/async. I assumed that the multithreading works like it does in NodeJS or Python, that is, it doesn't, everything just runs on the same thread. But I've been trying to learn how Tasks actually get executed and my understanding is that they're executed by TaskScheduler.Default who's implementation is hidden but can be expected to use a ThreadPool. Should I be programming as if all my Tasks can run in any thread? The extent of my asynchronous programming is

Why does the OnlyOnCanceled continuation get called?

元气小坏坏 提交于 2021-02-05 07:23:10
问题 When calling await RunAsync(); on the below code, I would expect the continuation with TaskContinuationOptions.OnlyRanToCompletion continuation to run, however the OnlyOnCanceled continuation gets called (yielding the debug output "Task canceled"). Why? private static async Task RunAsync() { try { await Task.Run(() => DoWork()) .ContinueWith( (t) => { if (t?.Exception != null) { throw t.Exception; } }, TaskContinuationOptions.OnlyOnFaulted ).ContinueWith( (t) => { Debug.WriteLine("Task

Difference between using Task.Yield() in a User Interface and Console App

房东的猫 提交于 2021-02-05 07:23:10
问题 I'm trying to asynchronously show a progress form that says the application is running while the actual application is running. As following this question, I have the following: Main Form: public partial class MainForm : Form { public MainForm() { InitializeComponent(); } async Task<int> LoadDataAsync() { await Task.Delay(2000); return 42; } private async void Run_Click(object sender, EventArgs e) { var runningForm = new RunningForm(); runningForm.ShowRunning(); var progressFormTask =

Difference between using Task.Yield() in a User Interface and Console App

时光怂恿深爱的人放手 提交于 2021-02-05 07:22:27
问题 I'm trying to asynchronously show a progress form that says the application is running while the actual application is running. As following this question, I have the following: Main Form: public partial class MainForm : Form { public MainForm() { InitializeComponent(); } async Task<int> LoadDataAsync() { await Task.Delay(2000); return 42; } private async void Run_Click(object sender, EventArgs e) { var runningForm = new RunningForm(); runningForm.ShowRunning(); var progressFormTask =

Why is .json() asynchronous? [duplicate]

末鹿安然 提交于 2021-02-05 05:52:28
问题 This question already has answers here : Why does .json() return a promise? (4 answers) Closed last year . I've been following a tutorial and came across the following code snippet: const myAsyncFunction = async () => { const usersResponse = await fetch( 'https://jsonplaceholder.typicode.com/users' ) const userJson = await usersResponse.json(); const secondUser = userJson[1]; console.log(secondUser); const posts = await fetch ( 'https://jsonplaceholder.typicode.com/posts?userId=' + secondUser

How to find out which of these sites failed

旧街凉风 提交于 2021-02-05 05:41:03
问题 I have an asyncronous task that goes out to several websites parses some data and returns the data when its finished. In the event that one or more of the websites fail - I need to find out which ones failed. Is this possible? Here is some example code I have: The controller public async Task<ActionResult> Index() { Models.WebSite ws = new Models.WebSite(); List<Task<string>> webList = new List<Task<string>>(); webList.Add(ws.GetWebsiteInfoAsync("http://website1.com")); webList.Add(ws

How to find out which of these sites failed

耗尽温柔 提交于 2021-02-05 05:39:10
问题 I have an asyncronous task that goes out to several websites parses some data and returns the data when its finished. In the event that one or more of the websites fail - I need to find out which ones failed. Is this possible? Here is some example code I have: The controller public async Task<ActionResult> Index() { Models.WebSite ws = new Models.WebSite(); List<Task<string>> webList = new List<Task<string>>(); webList.Add(ws.GetWebsiteInfoAsync("http://website1.com")); webList.Add(ws

How to find out which of these sites failed

微笑、不失礼 提交于 2021-02-05 05:39:08
问题 I have an asyncronous task that goes out to several websites parses some data and returns the data when its finished. In the event that one or more of the websites fail - I need to find out which ones failed. Is this possible? Here is some example code I have: The controller public async Task<ActionResult> Index() { Models.WebSite ws = new Models.WebSite(); List<Task<string>> webList = new List<Task<string>>(); webList.Add(ws.GetWebsiteInfoAsync("http://website1.com")); webList.Add(ws

How can I insert a delay between a List of Task<int>?

自古美人都是妖i 提交于 2021-02-05 05:23:55
问题 I have a loop that creates 5 Tasks. How can I insert a Delay of 5 seconds between each Task. I don't know how to fit Task.Delay(5000) in there. var tasks = new List<Task<int>>(); for (var i = 0; i < 5; i++) { tasks.Add(ProcessQueueAsync()); } await Task.WhenAll(tasks); My ProcessQueAsync method calls a server, retrieves data and returns and int. private async Task<int> ProcessQueAsync() { var result = await CallToServer(); return result.Count; } 回答1: for (var i = 0; i < 5; i++) { tasks.Add

Lazy<Task<T>> with asynchronous initialization

限于喜欢 提交于 2021-02-04 19:40:30
问题 class Laziness { static string cmdText = null; static SqlConnection conn = null; Lazy<Task<Person>> person = new Lazy<Task<Person>>(async () => { using (var cmd = new SqlCommand(cmdText, conn)) using (var reader = await cmd.ExecuteReaderAsync()) { if (await reader.ReadAsync()) { string firstName = reader["first_name"].ToString(); string lastName = reader["last_name"].ToString(); return new Person(firstName, lastName); } } throw new Exception("Failed to fetch Person"); }); public async Task