0

I am working in c# winform application.

I have this action code on a button click event:

    private int liveRATE = 0;
    private async void GetLiveRate()
    {                        
        var productResponseInitialStep = await productClient.GetProductTickerAsync(Currency);
        if (productResponseInitialStep.StatusCode == HttpStatusCode.OK)
        {                
            liveRATE = 100;
        }

        if (liveRATE > 0)
            SendInitialPO(liveRATE);        
    }

Which is working as expected. and SendInitialPO is working.

Now I am trying to push few lines to a function so I can reuse it without writing few lines again and again. But its not working as I expected which is the code never touching SendInitialPO(liveRATE);.

    private int liveRATE = 0;
    private async void GetLiveRate()
    {                                   
        GetLiveRate2(); 
        if (liveRATE > 0)
            SendInitialPO(liveRATE);        
    }       

    private async void GetLiveRate2()
    {            
        var productResponseInitialStep = await productClient.GetProductTickerAsync(Currency);                
        if (productResponseInitialStep.StatusCode == HttpStatusCode.OK)
        {
            liveRATE = 100;
        }            
    }

I was reading about how aync and await works. but not sure how can I solve my issue. I have tried to add do while loop but no luck. Any help would be appreciable.

6
  • 3
    await GetLiveRate(); more async Task GetLiveRate() Commented Mar 15, 2018 at 18:06
  • @brykneval But first they have to make it return a Task Commented Mar 15, 2018 at 18:07
  • 3
    You have 2 functions with the same signature? Commented Mar 15, 2018 at 18:07
  • 1
    If it's async you typically return Task instead of void. Also, you're not actually using await on anything in the first GetLiveRate(). Also, why do you have two functions with the same name? Commented Mar 15, 2018 at 18:07
  • 3
    You really need to read over Async Await Best Practices. For anyone that has done so, async void screams out that it's an issue. And instead of storing your results in a variable, you should return them from your method. Use a more functional programming pattern instead of storing state in fields that doesn't need to be. Commented Mar 15, 2018 at 18:21

1 Answer 1

1

Try this:

private int liveRATE = 0;
private async Task GetLiveRate()
{                                   
    await GetLiveRateAsync(); 
    if (liveRATE > 0)
        SendInitialPO(liveRATE);        
}       

private async Task GetLiveRateAsync()
{            
    var productResponseInitialStep = await productClient.GetProductTickerAsync(Currency);                
    if (productResponseInitialStep.StatusCode == HttpStatusCode.OK)
    {
        liveRATE = 100;
    }            
}

With some best practices I would recommend following code block: (see the subtle change of return type and variable declarations)

private async Task GetLiveRate()
{                                   
    var liveRATE = await GetLiveRateAsync(); 
    if (liveRATE > 0)
        SendInitialPO(liveRATE);        
}       

private async Task<int> GetLiveRateAsync()
{            
    var productResponseInitialStep = await productClient.GetProductTickerAsync(Currency);                
    if (productResponseInitialStep.StatusCode == HttpStatusCode.OK)
      return 100;

    return 0            
}

anyway, the main takeaway is: async void is okay for event handlers only. You should return a Task and calls await when required.

For more details please refer documentation.

An async method can have the following return types:

  • Task
  • Task<TResult>
  • void, which should only be used for event handlers.
  • Starting with C# 7, any type that has an accessible GetAwaiter method. The System.Threading.Tasks.ValueTask<TResult> type is one
    such implementation. It is available by adding the NuGet package
    System.Threading.Tasks.Extensions.
Sign up to request clarification or add additional context in comments.

1 Comment

@user1747541 glad that helped you. See the updated answer to get an idea on better way to code that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.