C # Early identification of actor Model

Source: Internet
Author: User

Recently learned some of the C # dataflow some things, and then a person abroad, with dataflow to achieve, an Actor model;

Here is a comparison, is the first knowledge of our actor model, and then we further in-depth understanding of a ha;

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Threading.Tasks.Dataflow;namespaceactor_model{//basic use and examples of actor model;//https://blog.jayway.com/2013/11/15/an-actor-model-implementation-in-c-using-tpl-dataflow/#comment -463337     Public Abstract classMessage {}//all actions are expressed in the form of Meg;//we all act as a message, let the message pass out, the real execution, to our specific actor to execute the drip;     Public classDeposit:message { Public decimalAmount {Get;Set; } }    //To check the balance we send a querybalance message     Public classQuerybalance:message {//The result of the query should is sent as a message to another actor//In other words, the information is given to another actor to deal with the drop;         PublicActor Receiver {Get;Set; } }    //The final message is the result of the balance Chec     Public classBalance:message { Public decimalAmount {Get;Set; } }     Public Abstract classActor {Private ReadOnlyActionblock<message>_action;  PublicActor () {_action=Newactionblock<message> (msg =              {                  DynamicSelf = This; DynamicMess =msg; Self.              Handle (mess);        })); }         Public voidSend (Message msg) {_action.        Post (msg); }         PublicTask Completion {Get{_action.complete (); return_action.completion; }        }    }    //Each account has a balance. We want to is able to deposit money and check the balance//the performer of the actual action;     Public classAccountactor:actor {Private decimal_balance; //Save         Public voidHandle (Deposit msg) {_balance+ = Msg. Amount;//Deposite Money        }        //Check Balance         Public voidHandle (querybalance msg) {//Check out the balance, called to our new actor to deal with the amount;//equivalent to creating a new ActoMsg. Receiver.send (NewBalance {Amount =_balance}); }    }    //We also need an actor that outputs the result of the balance query     Public classOutputactor:actor { Public voidHandle (Balance msg) {Console.WriteLine ("Balance is {0}", Msg.        Amount); }    }    classProgram {Static voidMain (string[] args) {            varAccount =NewAccountactor (); varOutput =NewOutputactor (); Account. Send (NewDeposit {Amount = - }); Account. Send (Newquerybalance {Receiver =output});            Account.Completion.Wait ();            Output.Completion.Wait (); Console.WriteLine ("done!");        Console.ReadLine (); }    }}

After reading the basic understanding actor, but still very not thorough ~

C # Early identification of actor Model

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.