I would like to write an Event Handler which triggers a method when website returns some response.
My application fetch response by posting URL's from few websites. Unfortunately at times website return response after some delay(It may take 1 to 5 seconds) and that leads my application to throw an error because next request executes without waiting for previous request to get response.
I can actually put a sleep time after every request that application posts but that doesn't seems to be right way because if I set 5 seconds as sleep time and if suppose website returns response in 1 seconds that makes process to wait for 4 more seconds unnecessarily.
To save some processing time I decide to add Event handlers which should allow application to run next request after we get response for previous request.
So I tried something like this and I can able to call trigger but it is not working the way I want.
My intention is to to create trigger which makes next request to run after getting response to the previous request and at most it can wait 5 second.
Can someone please help me in this, Thanks In advance.
public delegate void ChangedEventHandler(string response);
public class ListWithChangedEvent : EventArgs
{
public event ChangedEventHandler Changed;
protected virtual void OnChanged(string response)
{
if (Changed != null)
{
Changed(response);
}
}
public void Validate(string response)
{
OnChanged(response);
}
}
public class EventListener
{
private ListWithChangedEvent _list;
public EventListener(ListWithChangedEvent list)
{
_list = list;
_list.Changed += new ChangedEventHandler(ListChanged);
}
private void ListChanged(string response)
{
if (!response.IsEmpty())
{
return;
}
}
}
//Validating Response after request being posted
private void _postRequestAndParseResponse()
{
_performPostRequest(_returnTailPart(_urls.CommonUrl), argumentsList);
ListWithChangedEvent list = new ListWithChangedEvent();
EventListener listener = new EventListener(list);
list.Validate(_docNode.InnerHtml);
}