async code block #3378
Replies: 16 comments
-
What would it do that can't be achieved with |
Beta Was this translation helpful? Give feedback.
-
because i would want to write less code everytime i want to do something async also for long running tasks, you could do as such
this would remove a lot of the brackets needed |
Beta Was this translation helpful? Give feedback.
-
The |
Beta Was this translation helpful? Give feedback.
-
i'm not proposing that it should start a new thread, i'm saying that |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
well i was saying this because it would look better and be faster to code, not saying "change how tasks and threads work" just adding the option of doing such => static void LongRunningThing<T>(List<string> ids)
{
var tl = new List<Task<List<T>>>();
// a task declared with "async{}" would start on the running thread
//start new thread with default options
var thr = thread{
foreach (var id in ids) {
//just like starting a delegate, this task would run on thread "thr"
var task = async {
return Db.Collection.SelectAllWithKey(id).ToList(); }
tl.Add(task);
}
}
//this executes on main thread
async{
Task.WhenAll(tl);
foreach (var task in tl)
{
//do updates to documents/rows based on data
}
}
} |
Beta Was this translation helpful? Give feedback.
-
If anything, this example is making the feature even less appealing. What is I'd highly suggest reading up on how async works in C#, because this looks largely incoherent in that context. |
Beta Was this translation helpful? Give feedback.
-
@ctigrisht Can you provide the equivalent code to your example, but using current C# syntax? I'm struggling to figure out what the Please don't use phrases like "this task would run on thread thr". |
Beta Was this translation helpful? Give feedback.
-
Rust and Scala have async blocks, and they're actually reasonably useful. Essentially Task M()
{
var task1 = async {
await whatever;
dowhatever()
};
var task2 = async {
await whatever2;
dowhatever2()
};
return Task.WhenAll(task1, task2);
} Is semantically equivalent to: Task M()
{
var task1 = ((Func<Task>)(async () => {
await whatever;
dowhatever()
}))();
var task2 = ((Func<Task>)(async () => {
await whatever2;
dowhatever2();
}))();
return Task.WhenAll(task1, task2);
} It makes more sense in languages where blocks are expressions though, and also where async is a modifier after the method signature before the method implementation. |
Beta Was this translation helpful? Give feedback.
-
If this were done, it probably also ought to be done for iterators as well. |
Beta Was this translation helpful? Give feedback.
-
I totally agree with you, which makes me wonder why they bothered adding expression-bodied members to the language. It's probably the most pointless language feature ever added to the language. |
Beta Was this translation helpful? Give feedback.
-
static void Method(List<string> ids)
{
var tl = new List<Task<List<object>>>();
var thr = new Thread(new ThreadStart(delegate
{
foreach (var id in ids)
{
var task = Task.Run(delegate
{
//say this is db query
return new List<object>();
});
tl.Add(task);
}
}));
thr.Start();
Task.Run(delegate
{
foreach (var task in tl)
{
//do work with result
var res = task.Result;
}
});
} versus what i propose (mind that static void LongRunningThing(List<string> ids)
{
var tl = new List<Task<List<object>>>();
// a task declared with "async{}" would start on the running thread
//start new thread with default options
var thr = thread{
foreach (var id in ids)
{
//just like starting a delegate, this task would run on thread "thr"
var task = async {
//say this is db query
return new List<object>();
}
tl.Add(task);
}
}
thr.Start()
//this executes on main thread
async{
foreach (var task in tl)
{
//do work with result
var res = task.Result;
}
}
} also don't mind that the tasks are run on the main scheduler, even from the new thread, its just for display purposes |
Beta Was this translation helpful? Give feedback.
-
Race condition aside, these snippets are not equivalent. I'm having trouble seeing any tangible benefit that can be obtained with this feature, especially when the examples showcasing it don't seem to make much sense. |
Beta Was this translation helpful? Give feedback.
-
@ctigrisht The thing is, noone would ever write the code in your first snippet, so I think you're trying to solve a problem that doesn't exist. There's no point in Most people would write that as some variation on this (depending on whether the DB queries need to be made sequentially or in order, whether the DB query can be made asynchronous or not, etc): static async Task Method(List<string> ids)
{
var tasks = ids.Select(x => Db.Collection.SelectAllWithKeyAsync(x)).ToList();
await Task.WhenAll(tasks);
foreach (var task in tasks)
{
var result = task.Result;
// ...
}
} From your posts, I suspect that you don't understand how async/await works in C#. |
Beta Was this translation helpful? Give feedback.
-
@canton7 Unless the name of your DBMS starts with O, then your async calls are actually sync. |
Beta Was this translation helpful? Give feedback.
-
@orthoxerox Sure ^^. Then |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Would it be possible to add an
async
statement directly, not bound to anything, such as in this exampleasync{ }
would basically put the delegate/action in aTask.Run
at runtimeit would open code possibilities as such
Beta Was this translation helpful? Give feedback.
All reactions