S
Sudip_inn
Guest
I am getting this warning This async method lacks 'await' operators and will run synchronously
How to get rid of this warning ? where to use await keyword in my function body ? this is public async Task<ResponseContext> GetDataFromAPI(string tickername) the signature of the function where i need to use await keyword but i am not being able to use it. so guide me
This is my function code
public async Task<ResponseContext> GetDataFromAPI(string tickername)
{
string result = "";
//url will contain api url
string url = "";
//oResponseContext will store return data along with status & error details if fails
ResponseContext oResponseContext = new ResponseContext();
//allXfundData will store all 8 form data from url
List<XfundData> allXfundData = new List<XfundData>();
//WebClient will call the api by url
WebClient client = null;
//data will hold incoming data in stream format
Stream data = null;
//StreamReader will contain the stream of data
StreamReader reader = null;
//csv data will be deserialize to dataresult variable for each iteration
List<XfundData> dataresult = null;
//data will be fetch by 8 iteration & will store in alldata
List<string> alldata = null;
try
{
//iterate in 8 time to get data form no wise
//the form no passing also in url
for (int x = 0; x < 8; x++)
{
url = string.Format("{0}{1}{2}{3}", "http://nt3.zacks.com/xFund_EsAs/xFund_EsAs.asp?Master=", tickername, "&Form=", x.ToString());
//fetching data through WebClient
client = new WebClient();
data = client.OpenRead(url);
reader = new StreamReader(data);
//reading data and storing into string variable result
result = reader.ReadToEnd();
alldata = new List<string>();
//data is in CSV format so split line by new line
if (!String.IsNullOrEmpty(result) && result.Length > 12 && result.Contains(","))
{
alldata = result.Split(new string[] { Environment.NewLine }, StringSplitOptions.None).ToList();
}
dataresult = new List<XfundData>();
if (alldata.Any())
{
//parse CSV data by LINQ and result will be stored in alldata
dataresult = (from datas in alldata
where datas.Split(',').ToArray().Count() > 1
select new XfundData
{
Stand = datas.Split(',')[1],
StandardName = datas.Split(',')[2],
AsCode = datas.Split(',')[3],
AsReportedItemName = datas.Split(',')[4],
Scale = datas.Split(',')[5]
}).ToList();
}
if (dataresult.Any())
{
//finally data will be added to allXfundData for each iteration
allXfundData.AddRange(dataresult);
}
}
//storing data & status in ResponseContext class when success
oResponseContext.IsSuccess = true;
oResponseContext.Message = "Success";
oResponseContext.ResponseData = allXfundData;
}
catch(Exception ex)
{
//storing exception & message in ResponseContext class when fail
oResponseContext.IsSuccess = false;
oResponseContext.Message = "Error "+ex.Message.ToString();
oResponseContext.ResponseData = null;
}
finally
{
//disposing final list
allXfundData = null;
}
//return result in the form of ResponseContext
return oResponseContext;
}
XFundMapper oTickerList = new XFundMapper();
var task1 = oTickerList.GetDataFromAPI(cboTicker.Text);
await Task.WhenAll(task1);
ResponseContext oResponseContext = task1.Result;
if(oResponseContext.IsSuccess)
{
dgSourceFundamental.DataSource = oResponseContext.ResponseData;
}
else
{
MessageBox.Show(oResponseContext.Message);
}
Continue reading...
How to get rid of this warning ? where to use await keyword in my function body ? this is public async Task<ResponseContext> GetDataFromAPI(string tickername) the signature of the function where i need to use await keyword but i am not being able to use it. so guide me
This is my function code
public async Task<ResponseContext> GetDataFromAPI(string tickername)
{
string result = "";
//url will contain api url
string url = "";
//oResponseContext will store return data along with status & error details if fails
ResponseContext oResponseContext = new ResponseContext();
//allXfundData will store all 8 form data from url
List<XfundData> allXfundData = new List<XfundData>();
//WebClient will call the api by url
WebClient client = null;
//data will hold incoming data in stream format
Stream data = null;
//StreamReader will contain the stream of data
StreamReader reader = null;
//csv data will be deserialize to dataresult variable for each iteration
List<XfundData> dataresult = null;
//data will be fetch by 8 iteration & will store in alldata
List<string> alldata = null;
try
{
//iterate in 8 time to get data form no wise
//the form no passing also in url
for (int x = 0; x < 8; x++)
{
url = string.Format("{0}{1}{2}{3}", "http://nt3.zacks.com/xFund_EsAs/xFund_EsAs.asp?Master=", tickername, "&Form=", x.ToString());
//fetching data through WebClient
client = new WebClient();
data = client.OpenRead(url);
reader = new StreamReader(data);
//reading data and storing into string variable result
result = reader.ReadToEnd();
alldata = new List<string>();
//data is in CSV format so split line by new line
if (!String.IsNullOrEmpty(result) && result.Length > 12 && result.Contains(","))
{
alldata = result.Split(new string[] { Environment.NewLine }, StringSplitOptions.None).ToList();
}
dataresult = new List<XfundData>();
if (alldata.Any())
{
//parse CSV data by LINQ and result will be stored in alldata
dataresult = (from datas in alldata
where datas.Split(',').ToArray().Count() > 1
select new XfundData
{
Stand = datas.Split(',')[1],
StandardName = datas.Split(',')[2],
AsCode = datas.Split(',')[3],
AsReportedItemName = datas.Split(',')[4],
Scale = datas.Split(',')[5]
}).ToList();
}
if (dataresult.Any())
{
//finally data will be added to allXfundData for each iteration
allXfundData.AddRange(dataresult);
}
}
//storing data & status in ResponseContext class when success
oResponseContext.IsSuccess = true;
oResponseContext.Message = "Success";
oResponseContext.ResponseData = allXfundData;
}
catch(Exception ex)
{
//storing exception & message in ResponseContext class when fail
oResponseContext.IsSuccess = false;
oResponseContext.Message = "Error "+ex.Message.ToString();
oResponseContext.ResponseData = null;
}
finally
{
//disposing final list
allXfundData = null;
}
//return result in the form of ResponseContext
return oResponseContext;
}
XFundMapper oTickerList = new XFundMapper();
var task1 = oTickerList.GetDataFromAPI(cboTicker.Text);
await Task.WhenAll(task1);
ResponseContext oResponseContext = task1.Result;
if(oResponseContext.IsSuccess)
{
dgSourceFundamental.DataSource = oResponseContext.ResponseData;
}
else
{
MessageBox.Show(oResponseContext.Message);
}
Continue reading...