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 .");
}
}
}