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.
Taskasyncyou typically returnTaskinstead ofvoid. Also, you're not actually usingawaiton anything in the firstGetLiveRate(). Also, why do you have two functions with the same name?async voidscreams 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.