task

How should I convert a function returning a non-generic Task to ValueTask?

杀马特。学长 韩版系。学妹 提交于 2021-02-20 06:30:52
问题 I'm working on some code which builds a buffer in memory and then empties it into a TextWriter when the buffer fills up. Most of the time, the character will go straight into the buffer (synchronously) but occasionally (once every 4kb) I need to call TextWriter.WriteAsync . In the System.Threading.Tasks.Extensions package there only appears to be a ValueTask<T> struct, and no non-generic ValueTask (without a type parameter). Why is there no ValueTask , and what should I do if I need to

How should I convert a function returning a non-generic Task to ValueTask?

社会主义新天地 提交于 2021-02-20 06:28:50
问题 I'm working on some code which builds a buffer in memory and then empties it into a TextWriter when the buffer fills up. Most of the time, the character will go straight into the buffer (synchronously) but occasionally (once every 4kb) I need to call TextWriter.WriteAsync . In the System.Threading.Tasks.Extensions package there only appears to be a ValueTask<T> struct, and no non-generic ValueTask (without a type parameter). Why is there no ValueTask , and what should I do if I need to

“timer + Task.Run” vs “while loop + Task.Delay” in asp.net core hosted service

不羁岁月 提交于 2021-02-20 04:30:07
问题 I have a requirement that background service should run Process method every day at 0:00 a.m. So, one of my team member wrote the following code: public class MyBackgroundService : IHostedService, IDisposable { private readonly ILogger _logger; private Timer _timer; public MyBackgroundService(ILogger<MyBackgroundService> logger) { _logger = logger; } public void Dispose() { _timer?.Dispose(); } public Task StartAsync(CancellationToken cancellationToken) { TimeSpan interval = TimeSpan

Task stays in WaitingToRun state for abnormally long time

元气小坏坏 提交于 2021-02-18 17:44:10
问题 I've got a program that handles a variety of tasks running in parallel. A single task acts as a manager of sorts, making sure certain conditions are met before the next task is ran. However, I've found that sometimes a task will sit in the WaitingToRun state for a very long time. Here's the following code: mIsDisposed = false; mTasks = new BlockingCollection<TaskWrapper>(new ConcurrentQueue<TaskWrapper>()); Task.Factory.StartNew(() => { while (!mIsDisposed) { var tTask = mTasks.Take(); tTask

How to convert synchronous method to asynchronous without changing it's signature

别等时光非礼了梦想. 提交于 2021-02-16 22:47:11
问题 I have a large scale C# solution with 40-ish modules. I'm trying to convert a service used solution-wide from synchronous to asynchronous. the problem is I can't find a way to do so without changing the signature of the method. I've tried wrapping said asynchronous desired operation with Task but that requires changing the method signature. I've tried changing the caller to block itself while the method is operating but that screwed my system pretty good because it's a very long calling-chain

How to convert synchronous method to asynchronous without changing it's signature

感情迁移 提交于 2021-02-16 22:46:50
问题 I have a large scale C# solution with 40-ish modules. I'm trying to convert a service used solution-wide from synchronous to asynchronous. the problem is I can't find a way to do so without changing the signature of the method. I've tried wrapping said asynchronous desired operation with Task but that requires changing the method signature. I've tried changing the caller to block itself while the method is operating but that screwed my system pretty good because it's a very long calling-chain

Null reference - Task ContinueWith()

爷,独闯天下 提交于 2021-02-16 18:23:11
问题 For the following piece of code (.NET v4.0.30319) I am getting a null reference exception indicated below in the second continuation. Most interestingly this issue has only occurred in machines with 8GB RAM but other users have 16GB and more and they haven't reported any issue, and it is a very intermittent issue which leads me to suspect a garbage collection issue. The GetData() can be called multiple times so the first continuation of _businessObjectTask will only be called once as

How to queue a (macro)task in the JavaScript task queue?

时光总嘲笑我的痴心妄想 提交于 2021-02-16 05:29:58
问题 What is the most straight-forward and direct way to queue a task in the browser event loop, using JavaScript? Things that don't work: window.setImmediate(func): Non-standard. window.setTimeout(func, 0) / window.setInterval(func, 0) : Browsers throttle timers to ≥ 4ms. new Promise(r => r()).then(func) : That queues a microtask, not a task. 回答1: MessagePort.postMessage does just that. onmessage = e => handleMessage; postMessage("","*"); You can even use a MessageChannel if you want a less

Parallel tasks performance in c#

房东的猫 提交于 2021-02-11 17:25:42
问题 I need to make Tasks run faster, I tried to use semaphore, parallel library and threads(tried to open one for every work, I know its the most dumb thing to do), but none of them show the performance I need. I'm not familiar to work with thread stuff and I need some help to find the right way and understand how Task and Threads work. Here is the function: public class Test { public void openThreads() { int maxConcurrency = 500; var someWork = get_data_from_database(); using (SemaphoreSlim

Tasks combine result and continue

一曲冷凌霜 提交于 2021-02-11 12:41:01
问题 I have 16 tasks doing the same job, each of them return an array. I want to combine the results in pairs and do same job until I have only one task. I don't know what is the best way to do this. public static IComparatorNetwork[] Prune(IComparatorNetwork[] nets, int numTasks) { var tasks = new Task[numTasks]; var netsPerTask = nets.Length/numTasks; var start = 0; var concurrentSet = new ConcurrentBag<IComparatorNetwork>(); for(var i = 0; i < numTasks; i++) { IComparatorNetwork[] taskNets; if