惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

大猫的无限游戏
大猫的无限游戏
Webroot Blog
Webroot Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
T
Threat Research - Cisco Blogs
V2EX - 技术
V2EX - 技术
L
LINUX DO - 热门话题
Google DeepMind News
Google DeepMind News
Recorded Future
Recorded Future
S
Schneier on Security
I
InfoQ
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
The GitHub Blog
The GitHub Blog
S
Security @ Cisco Blogs
O
OpenAI News
W
WeLiveSecurity
Vercel News
Vercel News
阮一峰的网络日志
阮一峰的网络日志
Simon Willison's Weblog
Simon Willison's Weblog
人人都是产品经理
人人都是产品经理
Cloudbric
Cloudbric
The Last Watchdog
The Last Watchdog
The Hacker News
The Hacker News
Google Online Security Blog
Google Online Security Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
GbyAI
GbyAI
NISL@THU
NISL@THU
T
Tailwind CSS Blog
V
Visual Studio Blog
PCI Perspectives
PCI Perspectives
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Jina AI
Jina AI
D
DataBreaches.Net
B
Blog RSS Feed
N
News and Events Feed by Topic
N
News and Events Feed by Topic
H
Heimdal Security Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
腾讯CDC
Latest news
Latest news
V
Vulnerabilities – Threatpost
Hacker News: Ask HN
Hacker News: Ask HN
WordPress大学
WordPress大学
V
V2EX
aimingoo的专栏
aimingoo的专栏
博客园 - 司徒正美
Apple Machine Learning Research
Apple Machine Learning Research
D
Darknet – Hacking Tools, Hacker News & Cyber Security
The Register - Security
The Register - Security
Help Net Security
Help Net Security

博客园 - rapid

Rapid Business Development: Lightswitch vs. Dynamics CRM vs. SharePoint 2010 vs. ASP.NET MVC 3 OCS 2007 R2下载资源整理 下载silverlight官网的全部视频 [已修正链接] 对Scott Gu的Silverlight Sample--DiggSample的修改 - rapid 让Windows7顺利识别笔记本4G内存 “最郁闷的一次面试经历”勾起我对面试经验的一些看法 转:WF中的跟踪服务(1):Sql跟踪数据库表,视图,存储过程等相关说明 转:技巧:使用User Control做HTML生成 转:一点一点学ASP.NET之基础概念——HttpModule Increasing the CRM 4.0 Attachment Size Limit 超级实用且不花哨的js代码大全 (转)编写发布Plug-In 转:汉字转全拼,简拼组件 转:高阶函数、委托与匿名方法 转:.NET平台下Web测试工具横向比较 比较著名的.net技术论坛网址(含国外的) SMO学习笔记(一)——备份篇之完全备份 转:数据库管理对象(SMO)-为什么没有System.Data.DBManagement命名空间? 网站管理模板 stp 安装解决办法
OpenSesame示例源码
rapid · 2009-08-11 · via 博客园 - rapid

using System;

using System.Collections.Generic;

using System.Threading;

namespace OpenSesame

{

    public delegate void BookmarkLocation(Bookmark resumed);

    [Serializable]

    public class Bookmark

    {

        public Bookmark(string name, BookmarkLocation continueAt)

        {

            Name = name;

            ContinueAt = continueAt;

        }

        public string Name { get; set; }

        public BookmarkLocation ContinueAt { get; set; }

        public object Payload { get; set; }

        public BookmarkManager BookmarkManager { get; set; }

    }

    public class BookmarkManager

    {

        private List<Bookmark> bookmarkList;

        private ProgramStatement currentProgramStatement;

        public BookmarkManager()

        {

            bookmarkList = new List<Bookmark>();

        }

        public void Add(Bookmark bookmark)

        {

            bookmarkList.Add(bookmark);

            bookmark.BookmarkManager = this;

        }

        public void Remove(Bookmark bookmark)

        {

            bookmarkList.Remove(bookmark);

        }

        public void Resume(string bookmarkName, object payload)

        {

            foreach (Bookmark bookmark in bookmarkList)

            {

                if (bookmark.Name == bookmarkName)

                {

                    bookmark.Payload = payload;

                    bookmark.ContinueAt(bookmark);

                    break;

                }

            }

        }

        // Request execution of a program statement, using an

        // implicit bookmark that will be resumed when that

        // program statement completes its execution

        public void RunProgramStatement(ProgramStatement statement, BookmarkLocation continueAt)

        {

            currentProgramStatement = statement;

            Bookmark bookmark = new Bookmark(statement.GetType().FullName, continueAt);

            Add(bookmark);

            statement.Run(this);

        }

        // Indicate that the current program statement is done,

        // so that internally managed bookmarks can be resumed

        public void Done(bool bAllDone)

        {

            if (!bAllDone)

                Resume(currentProgramStatement.GetType().FullName, currentProgramStatement);

            else

                bookmarkList.Clear();

        }

    }

    [Serializable]

    public abstract class ProgramStatement

    {

        public abstract void Run(BookmarkManager mgr);

    }

    public class MythicalRuntime

    {

        Dictionary<ProgramHandle, ProgramStatement> ht;

        private BookmarkManager mgr = new BookmarkManager();

        public MythicalRuntime()

        {

            ht = new Dictionary<ProgramHandle, ProgramStatement>();

        }

        public BookmarkManager Mgr

        {

            get { return mgr; }

            set { mgr = value; }

        }

        public ProgramHandle RunProgram(ProgramStatement program)

        {

            //这个新的Guid根据规则创建,而不是简单的new Guid(),以下仅为模拟方法

            Guid programId = new Guid();

            ProgramHandle programHandle = new ProgramHandle();

            programHandle.ProgramId = programId;

            ht.Add(programHandle, program);

            //Bookmark的上半部分,用新的thread执行

            ThreadPool.QueueUserWorkItem(state => program.Run(state as BookmarkManager), Mgr);

            return programHandle;

        }

        public ProgramHandle GetProgramHandle(Guid programId)

        {

            //根据programId恢复已经钝化的程序,假设恢复为read方法

            ProgramStatement program = new Read();

            //重新构建ProgramHandle

            ProgramHandle programHandle = new ProgramHandle();

            programHandle.ProgramId = programId;

            //重新加载到内存

            ht.Add(programHandle, program);

            return programHandle;

        }

        public void Shutdown()

        {

            //从内存中取出所有ProgramHandle, 依次钝化

            foreach (ProgramHandle tmpProgramHandle in ht.Keys)

            {

                ProgramStatement program = ht[tmpProgramHandle];

                tmpProgramHandle.Passivate(program);

            }

            ht = null;

        }

    }

    public class ProgramHandle

    {

        private Guid programId;

        public Guid ProgramId

        {

            get { return programId; }

            set { programId = value; }

        }

        public void Passivate(ProgramStatement program)

        {

            //program根据关键字programId进行钝化

        }

        public void Resume(string bookmarkName, object payload)

        {

            BookmarkManager mgr = new BookmarkManager();

            mgr.Resume(bookmarkName, payload);

        }

    }

    [Serializable]

    public class Read : ProgramStatement

    {

        private string text;

        public string Text

        {

            get { return text; }

        }

        public override void Run(BookmarkManager mgr)

        {

            mgr.Add(new Bookmark("read", ContinueAt));

        }

        void ContinueAt(Bookmark resumed)

        {

            text = (string)resumed.Payload;

            BookmarkManager mgr = resumed.BookmarkManager;

            mgr.Remove(resumed);

            mgr.Done(false);

        }

    }

    [Serializable]

    public class PrintKey : ProgramStatement

    {

        private string key;

        public string Key

        {

            get { return key; }

        }

        public override void Run(BookmarkManager mgr)

        {

            // Print the key

            key = DateTime.Now.Millisecond.ToString();

            Console.WriteLine("here is your key: " + key);

            mgr.Done(false);

        }

    }

    [Serializable]

    public class PrintGreeting : ProgramStatement

    {

        private string key;

        public string Key

        {

            get { return key; }

            set { key = value; }

        }

        private string s;

        public string Input

        {

            get { return s; }

            set { s = value; }

        }

        public override void Run(BookmarkManager mgr)

        {

            //没有做特殊处理,key肯定是null.

            if (string.IsNullOrEmpty(key))

            {

                Console.WriteLine("OKOK");

                return;

            }

            // Print the greeting if the key is provided

            if (key.Equals(s))

                Console.WriteLine("hello, world");

            else

            {

                Console.WriteLine("Wrong key!");

            }

            mgr.Done(false);

        }

    }

    [Serializable]

    public class ProgramStatementBlock : ProgramStatement

    {

        int currentIndex;

        List<ProgramStatement> statements = new List<ProgramStatement>();

        public IList<ProgramStatement> Statements

        {

            get { return statements; }

        }

        public override void Run(BookmarkManager mgr)

        {

            currentIndex = 0;

            // Empty statement block

            if (statements.Count == 0)

                mgr.Done(true);

            else

                mgr.RunProgramStatement(statements[0], ContinueAt);

        }

        public void ContinueAt(Bookmark resumed)

        {

            BookmarkManager mgr = resumed.BookmarkManager;

            // If we've run all the statements, we're done

            if (++currentIndex == statements.Count)

                mgr.Done(true);

            else // Else, run the next statement

                mgr.RunProgramStatement(statements[currentIndex], ContinueAt);

        }

    }

    public class OpenSesame_v3

    {

        static void Main(string[] args)

        {

            ProgramStatementBlock openSesameProgram = new ProgramStatementBlock();

            PrintKey printKey = new PrintKey();

            Read read = new Read();

            PrintGreeting printGreeting = new PrintGreeting();

            printGreeting.Key = printKey.Key;

            printGreeting.Input = read.Text;

            openSesameProgram.Statements.Add(printKey);

            openSesameProgram.Statements.Add(read);

            openSesameProgram.Statements.Add(printGreeting);

            MythicalRuntime runtime = new MythicalRuntime();

            ProgramHandle handle = runtime.RunProgram(openSesameProgram);

            string s = Console.ReadLine();

            //Bookmark的下半部分,用新的thread执行

            ThreadPool.QueueUserWorkItem(state => runtime.Mgr.Resume("read", state), s);

            // keep the main thread running

            Console.ReadLine();

        }

    }

}