日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

OpenSesame示例源码

發布時間:2024/8/1 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 OpenSesame示例源码 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
《WF本質論》中的第一章例子


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();

??????? }

??? }

}


轉載于:https://www.cnblogs.com/kyorry/archive/2009/08/11/1544010.html

總結

以上是生活随笔為你收集整理的OpenSesame示例源码的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。