WF本质论第一章的代码

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

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

 

 

ContractedBlock.gif ExpandedBlockStart.gif 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>
    
/// 书签类
    
/// </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.");
        }
    }
}

转载于:https://www.cnblogs.com/zhonghua/archive/2008/08/18/1270248.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值