Questions tagged [task]
A task is an abstraction that is used to work with concurrency, it can denote operation that should be executed concurrently with the rest of a program. A task is a concurrent thread of execution in Ada and represents an asynchronous operation in .NET, also it corresponds to Threads in Java.
8,590
questions
497
votes
13
answers
414k
views
How to safely call an async method in C# without await
I have an async method which returns no data:
public async Task MyAsyncMethod()
{
// do some stuff async, don't return any data
}
I'm calling this from another method which returns some data:
...
438
votes
2
answers
430k
views
When correctly use Task.Run and when just async-await
I would like to ask you on your opinion about the correct architecture when to use Task.Run. I am experiencing laggy UI in our WPF .NET 4.5
application (with Caliburn Micro framework).
Basically I am ...
435
votes
3
answers
396k
views
Task vs Thread differences [duplicate]
There are two classes available in .NET: Task and Thread.
What is the difference between those classes?
When is it better to use Thread over Task (and vice-versa)?
406
votes
12
answers
209k
views
Awaiting multiple Tasks with different results
I have 3 tasks:
private async Task<Cat> FeedCat() {}
private async Task<House> SellHouse() {}
private async Task<Tesla> BuyCar() {}
They all need to run before my code can continue ...
386
votes
10
answers
74k
views
Why use async and return await, when you can return Task<T> directly?
Is there any scenario where writing method like this:
public async Task<SomeResult> DoSomethingAsync()
{
// Some synchronous code might or might not be here... //
return await ...
259
votes
7
answers
146k
views
What is the use for Task.FromResult<TResult> in C#
In C# and TPL (Task Parallel Library), the Task class represents an ongoing work that produces a value of type T.
I'd like to know what is the need for the Task.FromResult method ?
That is: In a ...
252
votes
6
answers
60k
views
Do rails rake tasks provide access to ActiveRecord models?
I am trying to create a custom rake task, but it seems I dont have access to my models. I thought this was something implicitly included with rails task.
I have the following code in lib/tasks/test....
249
votes
12
answers
205k
views
Deleting all pending tasks in celery / rabbitmq
How can I delete all pending tasks without knowing the task_id for each task?
245
votes
4
answers
166k
views
Parallel.ForEach vs Task.Run and Task.WhenAll
What are the differences between using Parallel.ForEach or Task.Run() to start a set of tasks asynchronously?
Version 1:
List<string> strings = new List<string> { "s1", "s2&...
240
votes
5
answers
119k
views
Task continuation on UI thread
Is there a 'standard' way to specify that a task continuation should run on the thread from which the initial task was created?
Currently I have the code below - it is working but keeping track of ...
190
votes
4
answers
225k
views
How to cancel a Task in await?
I'm playing with these Windows 8 WinRT tasks, and I'm trying to cancel a task using the method below, and it works to some point. The CancelNotification method DOES get called, which makes you think ...
166
votes
13
answers
206k
views
How do I abort/cancel TPL Tasks?
In a thread, I create some System.Threading.Task and start each task.
When I do a .Abort() to kill the thread, the tasks are not aborted.
How can I transmit the .Abort() to my tasks ?
160
votes
2
answers
77k
views
Difference between await and ContinueWith
Can someone explain if await and ContinueWith are synonymous or not in the following example. I'm trying to use TPL for the first time and have been reading all the documentation, but don't understand ...
158
votes
6
answers
98k
views
'await' works, but calling task.Result hangs/deadlocks
I have the following four tests and the last one hangs when I run it. Why does this happen:
[Test]
public void CheckOnceResultTest()
{
Assert.IsTrue(CheckStatus().Result);
}
[Test]
public async ...
155
votes
16
answers
137k
views
Run PHP Task Asynchronously
I work on a somewhat large web application, and the backend is mostly in PHP. There are several places in the code where I need to complete some task, but I don't want to make the user wait for the ...
154
votes
6
answers
100k
views
Should I worry about "This async method lacks 'await' operators and will run synchronously" warning
I have a interface which exposes some async methods. More specifically it has methods defined which return either Task or Task<T>. I am using the async/await keywords.
I am in the process of ...
133
votes
2
answers
72k
views
Await on a completed task same as task.Result?
I'm currently reading "Concurrency in C# Cookbook" by Stephen Cleary, and I noticed the following technique:
var completedTask = await Task.WhenAny(downloadTask, timeoutTask);
if (completedTask ==...
129
votes
9
answers
118k
views
How can I call an async method in Main?
public class test
{
public async Task Go()
{
await PrintAnswerToLife();
Console.WriteLine("done");
}
public async Task PrintAnswerToLife()
{
int answer = ...
123
votes
2
answers
205k
views
Await vs Task.Result in an Async Method [duplicate]
What's the difference between doing the following:
async Task<T> method(){
var r = await dynamodb.GetItemAsync(...)
return r.Item;
}
vs
async Task<T> method(){
var task = ...
121
votes
5
answers
127k
views
How do I enable TODO/FIXME/XXX task tags in Eclipse?
In all my years of using Eclipse, I never knew until now that TODO / FIXME / XXX comment tags are supposed to appear in the task list. Apparently this is something that is disabled by default because ...
113
votes
8
answers
224k
views
Task.Run with Parameter(s)?
I have implemented a simple Task.Factory.StartNew() and I wonder how can I do it with Task.Run() instead?
Here is the basic code:
Task.Factory.StartNew(new Action<object>(
(x) =>
{
// Do ...
111
votes
2
answers
70k
views
A Task's exception(s) were not observed either by Waiting on the Task or accessing its Exception property. As a result, the unobserved exception was
What does this mean and how to resolve it?
I am using TPL tasks.
The whole error
A Task's exception(s) were not observed either by Waiting on the Task or accessing its Exception property. As a ...
109
votes
1
answer
17k
views
Difference between OperationCanceledException and TaskCanceledException?
What is the difference between OperationCanceledException and TaskCanceledException? If I am using .NET 4.5 and using the async/await keywords, which one should I be looking to catch?
109
votes
7
answers
105k
views
Timer & TimerTask versus Thread + sleep in Java
I found similar questions asked here but there weren't answers to my satisfaction. So rephrasing the question again-
I have a task that needs to be done on a periodic basis (say 1 minute intervals). ...
104
votes
6
answers
105k
views
Promise equivalent in C#
In Scala there is a Promise class that could be used to complete a Future manually. I am looking for an alternative in C#.
I am writing a test and I want it to look it similar to this:
// var ...
103
votes
4
answers
198k
views
Platform.runLater and Task in JavaFX
I have been doing some research on this but I am still VERY confused to say the least.
Can anyone give me a concrete example of when to use Task and when to use Platform.runLater(Runnable);? What ...
97
votes
3
answers
112k
views
How to properly create and run concurrent tasks using python's asyncio module?
I am trying to properly understand and implement two concurrently running Task objects using Python 3's relatively new asyncio module.
In a nutshell, asyncio seems designed to handle asynchronous ...
96
votes
12
answers
258k
views
My C# application is returning 0xE0434352 to Windows Task Scheduler but it is not crashing
I have written a few C# apps that I have running via windows task scheduler. They are running successfully (as I can see from the log files that they are writing ) but windows task scheduler shows ...
94
votes
7
answers
98k
views
How to handle Task.Run Exception
I had a problem with catching the exception from Task.Run which was resolved by changing the code as follows. I'd like to know the difference between handling exceptions in these two ways :
In the ...
93
votes
4
answers
10k
views
Why is Task<T> not co-variant?
class ResultBase {}
class Result : ResultBase {}
Task<ResultBase> GetResult() {
return Task.FromResult(new Result());
}
The compiler tells me that it cannot implicitly convert Task<...
89
votes
7
answers
143k
views
What is the best way to seed a database in Rails?
I have a rake task that populates some initial data in my rails app. For example, countries, states, mobile carriers, etc.
The way I have it set up now, is I have a bunch of create statements in ...
89
votes
3
answers
38k
views
How do I add a high-priority TODO comment in Visual Studio?
Adding a comment such as this:
// TODO: Refactor this code
...creates a task in the Task List that I can view etc. There is a column labeled ! that lets you sort these tasks by priority.
How can I ...
89
votes
8
answers
433k
views
What is the 'realtime' process priority setting for?
From what I've read in the past, you're encouraged not to change the priority of your Windows applications programmatically, and if you do, you should never change them to 'Realtime'.
What does the '...
86
votes
4
answers
37k
views
When to use TaskCreationOptions.LongRunning?
I've wondered this for quite a while, but never really found the answer.
I understand that it's a hint for the task scheduler where the task will run on, and that the task scheduler can (or nowadays ...
86
votes
6
answers
10k
views
How can I prevent synchronous continuations on a Task?
I have some library (socket networking) code that provides a Task-based API for pending responses to requests, based on TaskCompletionSource<T>. However, there's an annoyance in the TPL in that ...
82
votes
1
answer
161k
views
await Task.Delay() vs. Task.Delay().Wait()
In C# I have the following two simple examples:
[Test]
public void TestWait()
{
var t = Task.Factory.StartNew(() =>
{
Console.WriteLine("Start");
Task.Delay(5000).Wait();
...
78
votes
4
answers
37k
views
Is Task.Factory.StartNew() guaranteed to use another thread than the calling thread?
I am starting a new task from a function but I would not want it to run on the same thread. I don't care which thread it runs on as long as it is a different one (so the information given in this ...
73
votes
12
answers
34k
views
Asynchronous Task.WhenAll with timeout
Is there a way in the new async dotnet 4.5 library to set a timeout on the Task.WhenAll method? I want to fetch several sources, and stop after say 5 seconds, and skip the sources that weren't ...
72
votes
6
answers
86k
views
Proper way of handling exception in task continuewith
Please have a look at the following code-
static void Main(string[] args)
{
// Get the task.
var task = Task.Factory.StartNew<int>(() => { return div(32, 0); });
// For error ...
71
votes
7
answers
72k
views
C# - ThreadPool vs Tasks
As some may have seen in .NET 4.0, they've added a new namespace System.Threading.Tasks which basically is what is means, a task. I've only been using it for a few days, from using ThreadPool.
Which ...
70
votes
11
answers
87k
views
How to limit the maximum number of parallel tasks in C#
I have a collection of 1000 input message to process. I'm looping the input collection and starting the new task for each message to get processed.
//Assume this messages collection contains 1000 ...
68
votes
6
answers
112k
views
What is the difference between a thread/process/task?
What is the difference between a thread/process/task?
68
votes
6
answers
194k
views
How do I wait until Task is finished in C#?
I want to send a request to a server and process the returned value:
private static string Send(int id)
{
Task<HttpResponseMessage> responseTask = client.GetAsync("aaaaa");
string ...
64
votes
9
answers
116k
views
Android: Cancel Async Task
I use an async task to upload an image and get some results.
While uploading the image I see a progress dialog, written in onPreExecute() method like this:
protected void onPreExecute() {
...
64
votes
3
answers
34k
views
What is the point of .NET 4.6's Task.CompletedTask?
This blog post mentions the new Task APIs, including a new Task.CompletedTask property introduced in .NET 4.6.
Why was this added? How is this better than, say, Task.FromResult(whatever)?
63
votes
2
answers
24k
views
Thread.Sleep vs Task.Delay?
I know that Thread.Sleep blocks a thread.
But does Task.Delay also block? Or is it just like Timer which uses one thread for all callbacks (when not overlapping)?
(this question doesn't cover the ...
61
votes
4
answers
45k
views
Celery task that runs more tasks
I am using celerybeat to kick off a primary task that kicks of a number of secondary tasks. I have both tasks written already.
Is there a way to easily do this? Does Celery allow for tasks to be run ...
58
votes
3
answers
44k
views
Waiting for async/await inside a task
I have this construct in my main(), which creates
var tasks = new List<Task>();
var t = Task.Factory.StartNew(
async () =>
{
Foo.Fim();
await Foo.DoBar();
});
/...
57
votes
4
answers
46k
views
Python - What is queue.task_done() used for?
I wrote a script that has multiple threads (created with threading.Thread) fetching URLs from a Queue using queue.get_nowait(), and then processing the HTML. I am new to multi-threaded programming, ...
57
votes
4
answers
57k
views
Gradle task check if property is defined
I have a Gradle task that executes a TestNG test suite.
I want to be able to pass a flag to the task in order to use a special TestNG XML suite file (or just use the default suite if the flag isn't ...