The code in chapter 1 of WF Essence

Source: Internet
Author: User

The master who wrote the book lost nothing and the code was not completely copied. As a result, I spent some time organizing the code. I want to thank a man on the Internet for organizing the code, unfortunately, I don't know his name.

Today I found these codes in my hard drive and decided to publish them for sharing. Hello everyone.

 

 

Code
Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. IO;
Using System. Runtime. Remoting. Messaging;

Namespace WF
{
Public class Program
{
Static void Main ()
{
BookmarkManager mgr = new BookmarkManager ();
OpenSesame OS = new OpenSesame ();
OS. Start (mgr );
Mgr. Resume ("read", null );
Console. WriteLine ("Press any key to continue ");
Console. ReadKey ();
}
}

/// <Summary>
/// Bookmarks
/// </Summary>
[Serializable]
Public class Bookmark
{
Private string name;
Private object payload;
Private BookmarkLocation bookmarkLocation;
Private BookmarkManager bookmarkManager;
Public Bookmark (string name, BookmarkLocation continueAt)
{
This. name = name;
This. bookmarkLocation = continueAt;
}
Public string Name {get {return this. name ;}}
Public BookmarkLocation ContinueAt {get {return this. bookmarkLocation ;}}
Public object Payload {get {return this. payload;} set {this. payload = value ;}}
/// <Summary>
/// Bookmarks
/// </Summary>
Public BookmarkManager {get {return this. bookmarkManager;} set {this. bookmarkManager = value ;}}
}
/// <Summary>
/// Bookmark position (delegate type)
/// </Summary>
/// <Param name = "resumed"> </param>
Public delegate void BookmarkLocation (Bookmark resumed );

Public class BookmarkManager
{
Private delegate string AsyncReadLine ();
Private IList <Bookmark> bookmarks;
Private int currentIndex =-1;
Public void Add (Bookmark bookmark)
{
If (this. bookmarks = null)
This. bookmarks = new List <Bookmark> ();
Bookmark. BookmarkManager = this;
Bookmarks. Add (bookmark );
}
Public void Remove (Bookmark bookmark)
{
If (this. bookmarks! = Null & this. bookmarks. Count> 0)
This. bookmarks. Remove (bookmark );
}
Public void Resume (string bookmarkName, object payload)
{
Foreach (Bookmark bk in this. bookmarks)
If (bk. Name. Equals (bookmarkName) & bk. ContinueAt! = Null)
{
This. currentIndex = this. bookmarks. IndexOf (bk );
BeginReadLine (new AsyncCallback (AsyncCallback), payload );
}
}
Private void AsyncCallback (IAsyncResult ar)
{
Bookmark bk = this. bookmarks [currentIndex];
Bk. Payload = EndReadLine (ar );
Bk. ContinueAt. Invoke (bk );
}
Private IAsyncResult BeginReadLine (AsyncCallback asyncCallback, object state)
{
AsyncReadLine asyncReadLine = new AsyncReadLine (Console. ReadLine );
Return asyncReadLine. BeginInvoke (asyncCallback, state );
}
Private string EndReadLine (IAsyncResult ar)
{
AsyncReadLine reader = (AsyncReadLine) (AsyncResult) ar). AsyncDelegate;
Return reader. EndInvoke (ar );
}
}

[Serializable]
Public class OpenSesame
{
Private string key;
Public void Start (BookmarkManager mgr)
{
This. key = DateTime. Now. Millisecond. ToString ();
Console. WriteLine ("Here is the key: {0}", key );
Mgr. Add (new Bookmark ("read", this. ContinueAt ));
}
Private void ContinueAt (Bookmark resumed)
{
String s = (string) resumed. Payload;
BookmarkManager mgr = resumed. BookmarkManager;
Mgr. Remove (resumed );
If (this. key. Equals (s ))
Console. WriteLine ("Welcome, my friends! ");
Else
Console. WriteLine ("Sorry, you press a wrong key .");
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.