The 'await' operator can only be used within an async lambda expression
You must mark your lambda expression as async
, like so:
messageDialog.Commands.Add(new UICommand("No", async (command) =>
{
await showSaveDialog();
}));
The 'await' operator can only be used with an async lambda expression
int processCount = await Task.Run<int>(() =>
Should be
int processCount = await Task.Run<int>(async () =>
Remember that a lambda is just shorthand for defining a method. So, your outer method is async
, but in this case you're trying to use await
within a lambda (which is a different method than your outer method). So your lambda must be marked async
as well.
The 'await' operator can only be used within an async lambda expression error
The problem is in the lambda expression you pass to the Select
. If you want to make use of the await
inside the type create, RegionBreif
, you have to pass an async
lambda expression as below:
Select(async r => new RegionBreif
{
IsServiceable = await RegionBreif.checkIfServicable(Context, r.SiteId, r.Id)
})
The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type
You have an await
ed expression in your (Quick)Watch Window; those inspectors don't support async code.
If you want to inspect your query's results, either:
- Advance the current instruction one line by pressing F10, then inspect
returnList
- Inspect the result of everything after
await
(without the variable assignment as well) and append a.Result
, so:_context.Alert.Include(c => c.Group).Where(x => x.UsersId == currentUser.Id).ToListAsync().Result;
(do note that this can cause other problems, especially with frozen threads or within synchronizationcontexed code, AFAIK, essentially deadlocking your debugger)- Alternatively, inspect the non-async variant:
_context.Alert.Include(c => c.Group).Where(x => x.UsersId == currentUser.Id).ToList()
Using Interlocked.Add to Task.Add when using async await
Your code has several problems. That specific compiler error is complaining that you're using await
inside a lambda function that is not marked async
. It's expecting something like this:
deleteBlobTask.Add(async () => { Interlocked.Add(ref deletedBlobCount, await DeleteBlobAsync(item)); });
I also got rid of the t
parameter, since you're not using it.
Notice that you were also missing a semicolon before the closing }
. In a one-liner lambda, you can only omit the semicolon if you omit the braces, like this:
deleteBlobTask.Add(async () => Interlocked.Add(ref deletedBlobCount, await DeleteBlobAsync(item)) );
However, that still won't compile because the lambda expression is a Func<Task>
: a function that returns a Task
. Really what you want is a Task
, or in other words, you want the result of running that function, not the function itself. As you have it, that function isn't being run.
To declare an inline function (a lambda) and run it at the same time, the syntax gets a little convoluted:
deleteBlobTask.Add(new Func<Task>(async () => Interlocked.Add(ref deletedBlobCount, await DeleteBlobAsync(item)))() );
Notice that you declare the Func<Task>
, then put ()
at the end, just like you would any method call.
So.... it's complicated. Are you sure all this is that much better than just:
foreach (var item in BlobURLList)
{
deletedBlobCount += await DeleteBlobAsync(item);
}
await operator giving issue when using with lambda
Instead of using Task.Factory.StartNew
which is not recommended and doesn't support async lambdas (which you don't even need here), use Task.Run
:
var profileOneTask = Task.Run(() => getProfileOne());
var profileTwoTask = Task.Run(() => getProfileTwo());
Notice that I changed the variable names to reflect what they actually are. They are tasks that may, at some point, have a result. They are not the result of those operations.
For the second problem, you declared _httpRequestPolicy
as an instance member when you should have declared it as a static member for it to be usable without an instance. As discussed in the comments, though, you could just make getProfileOne
and getProfileTwo
instance methods.
The 'await' operator can only be used within an async method
You are defining the method with async
word after the return type Task
, async
must be before Task
.
public async Task SetupPartnerPackAsync(SetupInformation info)
{
.
.
.
Related Topics
Comparing 2 Objects and Retrieve a List of Fields with Different Values
In C# Why Can't a Conditional Operator Implicitly Cast to a Nullable Type
Get the Decimal Part from a Double
Does Anyone Know of a Good C# API for Subversion
C#: How to Add Subitems in Listview
Linq to Xml - Update/Alter the Nodes of an Xml Document
Cannot Deserialize JSON Array into Type - JSON.Net
Get the Application Pool Identity Programmatically
Show Authentication Dialog in C# for Windows Vista/7
Detecting If Paste Event Occurred Inside a Rich Text Box
Why Is 16 Byte the Recommended Size for Struct in C#
Get All Types Implementing Specific Open Generic Type
C# Open File, Path Starting with %Userprofile%
How to Make Correct Date Format When Writing Data to Excel