-2

In the below code:

objStockcheck = async ObjPrice.getprice();           
objStockcheck = async ObjAvail.getAvailability();

I need async in both lines but it shows the following error:

Error CS0103 The name 'async' does not exist in the current context in line 1 and 2.

Although i put a semicolon at lines 1 and 2, it shows the following:

Error CS1002 ; expected demoservice in line 1 and 2.

Thanks in advance.

jsanalytics
  • 13,058
  • 4
  • 22
  • 43
priya
  • 5
  • 7
  • i think we're missing some context. could we see the rest of the method this code is from? – Timothy Groote Nov 09 '17 at 09:13
  • public class PriceandAvailability { public string account { get; set; }} – priya Nov 09 '17 at 09:18
  • 4
    That's not how you use async and await. I would reccomend reading up on this subject more. You mark the method itself as async like this public async Task MyMethod() and then you can call it in another asynchronous method and await the result of the task. Await MyMethod(). If this all sounds unfamiliar to you, you need to read up some more – Dave Nov 09 '17 at 09:18
  • visual studio professional 2017 – priya Nov 09 '17 at 09:20
  • **Async is used when you declare method** and you need to call that async method in another async method using **await** keyword **not with async** – Prasad Telkikar Nov 09 '17 at 09:27
  • In class price I have method called getprice that method should contain fields like account and password with the values.Where these fields should be stored in single object(objprice) of the price class. The objprice(with values of account and password)should pass into Integration layer class.Where getstockcheck is the method of Integration layer class should accept those fields in objprice(object of price class). Then it should store the account and password value in single object(getstockcheck) of Integration layer class asynchronously. So how can I program this is in c#. – priya Nov 09 '17 at 09:49

2 Answers2

0

As @Timothy Groote said, difficult with not much context but I would say that async keyword is supposed to be on the declaration of your method. When you call your method you should use "await" keyword

fitz_lucassen
  • 83
  • 1
  • 11
0

As @Dev has suggested, This is not how you use async await.

you need to declare an asynchronous method first. And then use await keyword to wait for the result or call to finish.

private async void PostToServer(Model Data)
{
   //Do complex task without returning anything.
   //Like posting data to server.
   //...Do something with model
  await context.SaveAsync();
}

async Task<ObjPrice> getprice()
{
    //Do complex task and return result.
    //Like waiting for some other event to update the price.
    return await WaitForPriceChangeAsync();
}

public async Task Bar()
{
    var objStockcheck = await WaitForPriceUpdate();         
    await PostToServer();
}

Read more about async-await here:

https://learn.microsoft.com/en-us/dotnet/csharp/async

How and When to use `async` and `await`

Edit: Creating asynchronous task

private async Task<string> RandomTaskAsync()
{
 var result=await Task.Run<String>(=>
  {
   //Doing any task here will run in asynchronously
    return HugeComputing();
  });
  return result;
}

This method can be awaited in other method calls.

Also, As suggested by Jon Hanna, use async void only for event handlers (as there is no alternative in that case). Using otherwise violates the TAP principle.

Code Name Jack
  • 2,856
  • 24
  • 40
  • There's no need for `PostToServer()` or `getprice()` to use `async` unless they also do `await`. `Bar()` does need `async` since it contains an `await` and should be `public async Task Bar()` unless there's a **very** good reason for be `public void async`; pretty much only that it's an event handler that has to be `void`. – Jon Hanna Nov 09 '17 at 09:45
  • True, TO show the full usage I should actually add one more method or a call.ex. PostToServer may have db update queries which have asynchronous implementations. – Code Name Jack Nov 09 '17 at 11:20
  • You haven't fixed the problem in `Bar()` and that `RandomTaskAsync()` has more problems, though would at least compile. There's little point doing `var result = await …` just to return the result immediately after aside from a slightly nicer stack trace on an exception. Better to just return the task. It's also unwise to do `Task.Run` to create an async method that isn't truly async. If that sort of async is useful or not to the caller can only really be known by the caller, which can just do the `Task.Run` itself. – Jon Hanna Nov 09 '17 at 15:18
  • Task.Run has helped in scenarios where some libraries don't have any async implementation and you don't block your control there. And Sorry I noticed the error in Bar now – Code Name Jack Nov 09 '17 at 16:01