So I have a async Web Request method with a callback:
private void DoWebRequest(string url, string titleToCheckFor,
Action<MyRequestState> callback)
{
MyRequestState result = new MyRequestState();
// ...
// a lot of logic and stuff, and eventually return either:
result.DoRedirect = false;
// or:
result.DoRedirect = true;
callback(result);
}
(Just to point it out up front, my WebRequest.AllowAutoRedirect is set to false for a number of reasons)
I don’t know before hand how many redirects I can expect, so I started:
private void MyWrapperCall(string url, string expectedTitle,
Action<MyRequestState> callback)
{
DoWebRequest(url, expectedTitle, new Action<MyRequestState>((a) =>
{
if (a.DoRedirect)
{
DoWebRequest(a.Redirect, expectedTitle, new Action<MyRequestState>((b) =>
{
if (b.DoRedirect)
{
DoWebRequest(b.Redirect, expectedTitle, callback);
}
}));
}
}));
}
And now I got a brain meltdown, how could I put this is in an iterative loop so it does the last callback back to the original caller if no ReDirects are needed anymore?
Store a reference to the recursing method, so it can call itself: