写书的那位大师丢三落四,代码没有拷贝完全,导致我花了些时间整理了这些代码,整理这些代码要感谢网上的一位老兄,可惜我不知道他的名字了。

今天在我的硬盘里发现了这些代码,决定发布上来共享。大家好才是真的好。

 

 

 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 WF本质论第一章的代码 ");
            Console.ReadKey();
        }        
    }

    
/// <summary>
    
/// 书签类
    
/// </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>
        
/// 书签位置属性
        
/// </summary>
        public BookmarkManager BookmarkManager { get { return this.bookmarkManager; } set { this.bookmarkManager = value; } }
    }
    
/// <summary>
    
/// 书签位置(委托类型)
    
/// </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.");
        }
    }
}

相关文章: