I have an API which gives the data according to the time range requested.I have created an event which will call the dataInfo (gets the information about stored data) for every 2 minutes.How to stop or block the event until the current execution is completed.
following is the method where data refresh handler is set.
public async Task DataInitialize(DataProperties dataProperties)
{
//DataInitialise method is called when the API is initialized and it gets the data information, then for every 2 minutes it raises an event to refresh the dataInfo.
var dataInfo = GetStoredata();
//set the datarefresh handler
this.refreshTimer = new System.Timers.Timer { Interval = TimeSpan.FromMinutes(2).TotalMilliseconds };
this.refreshTimer.Elapsed += async (sender, e) => await this.DataRefreshEventHandler(sender, e);
this.refreshTimer.AutoReset = true;
this.refreshTimer.Enabled = true;
await Task.CompletedTask;
}
public async Task DataRefreshEventHandler(object dataUpdateTimer, ElapsedEventArgs e)
{
var dataInfo = GetStoredata();
await Task.CompletedTask;
}
API calls GetResponse method which uses dataInfo to retrieve the data and send the response.
public async Task<string> GetResponse(dataInfo){
return await Task.FromResult(GetData(dataInfo));
}
if the API takes more than 2 minutes to send the response, the event will be raised and it gets different dataInfo which will make API to throw an exception.