Visual C# Developer Center > Visual C# Forums > Visual C# Language > Asynchronous processing based on state
Ask a questionAsk a question
 

AnswerAsynchronous processing based on state

  • Thursday, November 05, 2009 8:15 PMSimple Samples Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    I need to process data asynchronously, based on the current status or state of processing. In other words, when incoming data is available, the program needs to process the data based on what the program is doing. This is likely a common requirement for networking but I don't know how to find samples or articles or whatever.

    I have code that I think works but I think it is not the most elegant. I am iterested in suggestions. In my algorithm, I have a base class that has WhatNext and WhereFrom static members that are instances of the class, and a virtual function to do the processing needed for the current state. The WhereFrom is so that data can be passed from state to state. The WhatNext is the processing to be done when data is available. I set the WhatNext member to null when processing is to end. This is just sample code and does not actually process data; in the actual code, the Do function will write data out then somehow wait for incoming data; I have not yet decided where to do the wait part but I assume that is easy; probably in the main loop.

    This algorithm is sloppy in that the WhereFrom member assumes that the instance using it uses it properly, but for this, that is okay in the sense that it is acceptable for the developer to be able to be careful and such. There is more error-checking that could be added of course.

    Is there a better solution? For my requirements, this is not for general use so it does not need to be as fool-proof as software needs to be that is for general use.

    If I use the algorithm shown below, should I explicitly dispose of the WhereFrom object after use? I probably don't actually need the WhereFrom member and could (should) maintain the data some other way.

    public abstract class StatusBaseObject
    {
    	public static StatusBaseObject WhatNext;
    	public static StatusBaseObject WhereFrom;
    
    	public virtual void Do()
    	{
    		Console.WriteLine("Status base");
    	}
    }
    
    public class Status1Object : StatusBaseObject
    {
    	public string Data = "Status 1 data";
    
    	public override void Do()
    	{
    		Console.WriteLine("Status one");
    		WhatNext = new Status2Object();
    		WhereFrom = this;
    	}
    }
    
    public class Status2Object : StatusBaseObject
    {
    	public string Data = "Status 2 data";
    
    	public override void Do()
    	{
    		Status1Object s1 = WhereFrom as Status1Object;
    		if (s1 == null)
    			Console.WriteLine("Status two from ????");
    		else
    			Console.WriteLine("Status two; data: {0}", s1.Data);
    		WhatNext = new Status3Object();
    		WhereFrom = this;
    	}
    }
    
    public class Status3Object : StatusBaseObject
    {
    	public override void Do()
    	{
    		Console.WriteLine("Status three");
    		WhatNext = null;
    		WhereFrom = this;
    	}
    }
    
    class Program
    {
    	static void Main(string[] args)
    	{
    		StatusBaseObject.WhereFrom = null;
    		StatusBaseObject.WhatNext = new Status1Object();;
    		while (StatusBaseObject.WhatNext != null)
    			StatusBaseObject.WhatNext.Do();
    	}
    }
    

Answers

  • Thursday, November 05, 2009 9:02 PMRudedog2ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Get a hold of this book.

    Head First Design Patterns - O'Reilly Media

    You may want to look at the State Pattern. 
    Your code bears a strong resemblance to it, and so does your requirements.
    You can download the sample code on this page .  Look in the middle of the page, halfway down.

    Do you like Gumballs?  The demo code is for a Gumball Machine.
    The sample code by itself not as useful without the book. 
    You will not be disappointed with that investment.

    Hope this helps.

    Mark the best replies as answers. "Fooling computers since 1971."

All Replies

  • Thursday, November 05, 2009 9:02 PMRudedog2ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Get a hold of this book.

    Head First Design Patterns - O'Reilly Media

    You may want to look at the State Pattern. 
    Your code bears a strong resemblance to it, and so does your requirements.
    You can download the sample code on this page .  Look in the middle of the page, halfway down.

    Do you like Gumballs?  The demo code is for a Gumball Machine.
    The sample code by itself not as useful without the book. 
    You will not be disappointed with that investment.

    Hope this helps.

    Mark the best replies as answers. "Fooling computers since 1971."
  • Friday, November 06, 2009 3:42 AMSimple Samples Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Thank you and I have downloaded the code. There sure is a lot of code. I believe you that the book is a good investment. At the moment I can't afford to buy it but I can probably borrow a copy from a library.

    At least I was headed in a good direction.

    This is actually for a POP (email) reader; I assume you know what that is. A POP reader of message lists is amazingly easy but making it asynchronous is more work. Asynchronous is critical for recovery from bad servers and to efficiently handle multiple accounts. So the networking book might also have something such as this.


    Sam Hobbs; see my SimpleSamples.Info
  • Friday, November 06, 2009 3:44 AMSimple Samples Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I forgot to mention that I will keep this question open for a couple of days in case someone had more to offer but I think you have answered the question, at least to the extent of confirming I have a good start.
    Sam Hobbs; see my SimpleSamples.Info
  • Friday, November 06, 2009 7:16 PMRudedog2ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Have you found the Gum Ball machine yet?

    It is a little difficult to understand without the detailed explanations in the book.

    Mark the best replies as answers. "Fooling computers since 1971."
  • Friday, November 06, 2009 10:35 PMSimple Samples Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Yes I found the GumballMachine class and the State class, but I could not find the top-level code that runs it all. I see that the GumballMachine class has members for soldOutState, noQuarterState, hasQuarterState, soldState and a state. I looked at the GumballMonitor class but that does not help. I looked at the Proxy.GumballMachine and DeveloperTests projects but I don't see anything there.

    So yes of course having the book is critical. There is a lot of source code and a lot of it is not for the GumballMachine, otherwise it would be easier to find the main code. I will get a copy of the book when I can.

    Since you are asking, if you don't mind telling me what project to look at for the main logic, then that would help. Otherwise, I don't mind proceeding with the code I have so far (as above) and then improving it as needed.

    Oh, and I have been doing programming since around 1971 too.

  • Friday, November 06, 2009 11:12 PMRudedog2ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Look in the Project named "DeveloperTests".

    It contains one class.  Instantiate it, and call the methods.
    Press F11 to step through the code. 
    As you noted, having the book is very helpful because of the detailed explanations.

    Basically, the code emulates what is known as a 'State Machine'.
    It all starts with a turn of the crank, once and if, a quarter is inserted.
    After a certain number of cranks, an extra gumball is dispensed.

    The code simulates the crank, and the dispensing processes.
    The turning of the crank is the asynch event that changes the state of the machine.
    It must keep track of how many quarters are inserted for a single gumball, and when to dispense two to a 'winner'.



    Mark the best replies as answers. "Fooling computers since 1971."