|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
IntroductionThe article shows how to emulate web continuation in .NET framework. It makes the creation of complex flow in web applications possible. BackgroundA continuation is a representation of the execution state of a program at a certain point. Not all languages can support continuations. Saving of the state in web application gives additional difficulty. Iterator block was introduced in C# 2.0. It can be presented as a method body
that returns one of the generic enumerator (enumerable) interfaces and contains
at least one yield statement. Following example shows how iterator block is
working. The function saves all internal variables and returns control to caller
on static IEnumerable<int> Fibonacci(int n)
{
int previous = 0;
int current = 1;
for (int i = 0; i < n; i++)
{
yield return current;
int next = previous + current;
previous = current;
current = next;
}
}
...
// Prints 1 1 2 3 5 8 13 21 34 55
foreach (int i in Fibonacci(10)) Console.WriteLine(i);
The enumerator saves local variables and execution resume point in memory, but it doesn’t allow exporting and importing of the state to and from an external stream. It’s the reason why it cannot be used as true web continuation solution. Using the codeWeb continuation manager implemented as As example, we will implement number guessing game using natural execution flow. Let’s declare iterator block inside page code behind class: private IEnumerator<object> NumberGuess()
{
Random random = new Random(); // generate random number
int answer = random.Next(1, 101);
int attempts = 1;
mainMultiView.SetActiveView(guessView);
lastGuessPanel.Visible = false;
yield return null; // store and return control
while(guess != answer)
{
++attempts;
lastGuessPanel.Visible = true;
lastGuessLabel.Text = guess.ToString();
lowerLabel.Visible = guess < answer;
higherLabel.Visible = guess > answer;
yield return null; // store and return control
}
mainMultiView.SetActiveView(resultView);
guessedNumberLabel.Text = answer.ToString();
attemptsLabel.Text = attempts.ToString();
}
We have to register enumerator with controlManager.Register(NumberGuess());
controlManager.Call();
It will update continuation data in When next request is being processed continuation will restore itself form
Points of InterestFull description of C# 2.0 “unfolding” of iterator block into enumerator can
be found in “C# 2.0
Specification”. We can notice that the enumerator contains all the local
variables, pointer to class that declares that method (unless the method is
static), state and current returned value. The enumerator type is declared as
private nested class. The enumerator data can be imported and exported using
Special attention is required for objects that implements
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||