async-await

Should one use Task.Run within another Task?

别等时光非礼了梦想. 提交于 2021-02-08 07:55:52
问题 I have a bunch of work to do (either CPU-bound or IO-bound with no async interface to use) within a Task. I'm wondering if it's OK to just do all the non-async work within the task like this: async Task DoSomeStuff() { await SomethingAsync(); … DoCpuBoundWork(); … await SomethingElseAsync(); } or should I use Task.Run like this? async Task DoSomeStuff() { await SomethingAsync(); … await Task.Run(() => DoCpuBoundWork()); … await SomethingElseAsync(); } I know tasks aren't necessarily executed

Should one use Task.Run within another Task?

﹥>﹥吖頭↗ 提交于 2021-02-08 07:54:44
问题 I have a bunch of work to do (either CPU-bound or IO-bound with no async interface to use) within a Task. I'm wondering if it's OK to just do all the non-async work within the task like this: async Task DoSomeStuff() { await SomethingAsync(); … DoCpuBoundWork(); … await SomethingElseAsync(); } or should I use Task.Run like this? async Task DoSomeStuff() { await SomethingAsync(); … await Task.Run(() => DoCpuBoundWork()); … await SomethingElseAsync(); } I know tasks aren't necessarily executed

C# SSH.Net asynchronous command read and write

[亡魂溺海] 提交于 2021-02-08 07:23:02
问题 I am trying to write an SSH script for some routers/switches to read out information using SSH.Net. I want to build it asynchronous because they are pretty slow with high latency. I can connect and send a command, but if I try to read it out, I just get some blank lines. If I run the WriteLine() method like 7 times in a row, I see like 2 of the expected outputs. I hope you can help me. Here is my code: private static string _srv = "255.255.255.255"; private static string _usr = "looping";

async with" if the event loop is running

强颜欢笑 提交于 2021-02-08 06:51:05
问题 I'm writing my first telegram bot with telepot and telethon my main code is: import sys import asyncio import random import telepot import telepot.aio from telepot.aio.loop import MessageLoop from telepot.namedtuple import ReplyKeyboardMarkup, KeyboardButton, ReplyKeyboardRemove, ForceReply from telepot.namedtuple import InlineKeyboardMarkup, InlineKeyboardButton from telepot.namedtuple import InlineQueryResultArticle, InlineQueryResultPhoto, InputTextMessageContent async def on_chat_message

async with" if the event loop is running

那年仲夏 提交于 2021-02-08 06:50:46
问题 I'm writing my first telegram bot with telepot and telethon my main code is: import sys import asyncio import random import telepot import telepot.aio from telepot.aio.loop import MessageLoop from telepot.namedtuple import ReplyKeyboardMarkup, KeyboardButton, ReplyKeyboardRemove, ForceReply from telepot.namedtuple import InlineKeyboardMarkup, InlineKeyboardButton from telepot.namedtuple import InlineQueryResultArticle, InlineQueryResultPhoto, InputTextMessageContent async def on_chat_message

Awaiting Socket Operations

狂风中的少年 提交于 2021-02-08 06:43:28
问题 Given the following code, modified from Stephen Toub's article. http://blogs.msdn.com/b/pfxteam/archive/2011/12/15/10248293.aspx public async Task Start(CancellationToken token) { while (!token.IsCancellationRequested) { await this.acceptCount.WaitAsync(token).ConfigureAwait(false); if (token.IsCancellationRequested) break; var args = new SocketAsyncEventArgs(); args.UserToken = new SocketFrame { CancellationToken = token, Buffer = null, Message = null, }; // How do I manage this additional

Contradictory advice regarding async / await in MVC

*爱你&永不变心* 提交于 2021-02-08 06:31:56
问题 I've been developing some library code that will be consumed by MVC action methods, so I've been reading a lot of Steven Cleary's blogs about the subject. I ran in to his "Don't Block on Async Code" blog post, but am confused about some parts that seem contradictory. Under "Preventing the Deadlock" he states: There are two best practices (both covered in my intro post) that avoid this situation: In your “library” async methods, use ConfigureAwait(false) wherever possible. Don’t block on Tasks

Task cancellation with async task

旧时模样 提交于 2021-02-08 05:27:34
问题 I'm trying to make use of cancellation tokens as described in this FAQ. This was my initial thought: private async void OnLoginButtonClicked(object sender, EventArgs e) { if (this.cancelToken == null) { this.cancelToken = new CancellationTokenSource(); } try { bool loginSuccess = await AsyncLoginTask(this.cancelToken.Token); if (loginSuccess) { // Show main page } } catch (OperationCanceledException ex) { System.Diagnostics.Debug.WriteLine(ex.Message); } catch (Exception ex) { System

Why is writing ConfigureAwait(false) on every line with await always recommended and do I really need it?

佐手、 提交于 2021-02-08 05:17:08
问题 The question is not about what ConfigureAwait does. But rather why literally everywhere I see something like As a general rule, yes. ConfigureAwait(false) should be used for every await unless the method needs its context. I.e. they propose that I should write await Method1().ConfigureAwait(false); await Method2().ConfigureAwait(false); // Do something else // ... await Method3().ConfigureAwait(false); await Method4().ConfigureAwait(false); But in such case wouldn't be clearer just resetting

Why is writing ConfigureAwait(false) on every line with await always recommended and do I really need it?

三世轮回 提交于 2021-02-08 05:15:46
问题 The question is not about what ConfigureAwait does. But rather why literally everywhere I see something like As a general rule, yes. ConfigureAwait(false) should be used for every await unless the method needs its context. I.e. they propose that I should write await Method1().ConfigureAwait(false); await Method2().ConfigureAwait(false); // Do something else // ... await Method3().ConfigureAwait(false); await Method4().ConfigureAwait(false); But in such case wouldn't be clearer just resetting