usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.IO;usingSocketim;usingSystem.Net;usingSystem.Net.Sockets;usingConsoleApplication1;usingSystem.Runtime.Remoting.Messaging;usingSystem.Threading;namespaceconsoleapplication2{ Public InterfaceIimcallback {voidImcallback (byte[] data); } Public classServerhandler:iimcallback { Public voidImcallback (byte[] data) { stringstr =System.Text.Encoding.UTF8.GetString (data); varDIC = newtonsoft.json.jsonconvert.deserializeobject<dictionary<string,string>>(str); if(dic["MessageType"] =="1") {Console.WriteLine (1); } Else if(dic["MessageType"] =="2") {Console.WriteLine (2); } System.Threading.Thread Thread=NewSystem.Threading.Thread (startlistening); Thread. IsBackground=true; Thread. Start (); } Public Static voidstartlistening () {//byte[] bytes = new byte[1024];IPAddress IPAddress= Ipaddress.parse ("127.0.0.1"); IPEndPoint Localendpoint=NewIPEndPoint (IpAddress,11000); //Create A TCP/IP socket. Socket listener =Newsockets (AddressFamily.InterNetwork, SocketType.Stream, protocoltype.tcp); //Bind the socket to the local//endpoint and listen for incoming connections. Try{listener. Bind (Localendpoint); Listener. Listen ( -); while(true) {listener. BeginAccept (NewAsyncCallback (acceptcallback), listener); } } Catch(Exception e) {Console.WriteLine (e.tostring ()); } Console.WriteLine ("\npress ENTER to continue ..."); Console.read (); } Public Static voidacceptcallback (IAsyncResult ar) {Socket listener=(Socket) ar. asyncstate; Socket Handler=Listener. Endaccept (AR); byte[] Buff =New byte[1024x768]; while(true) {handler. BeginReceive (Buff,0,1024x768,0,NewAsyncCallback (Readcallback), handler); } } Public Static voidreadcallback (IAsyncResult ar) {String content=String.Empty; //Retrieve the state object and the handler socket//From the asynchronous state object. Socket State =(Socket) ar. asyncstate; //Read data from the client socket. intBytesread =State . EndReceive (AR); byte[] Buff =New byte[1024x768]; } }} Public Delegate intAddevent (intAintb);classprogram{ Public classStateObject { Public stringKey {Get;Set; } Public BOOLState {Get;Set; } } Public Staticlist<int> bags =Newlist<int>(); Static voidMain (string[] args) {ManualResetEvent T=NewManualResetEvent (false); Dictionary<int, stateobject> Dicdone =Newdictionary<int, stateobject>(); Dicdone.add (1,NewStateObject () {key="1", state=false }); Dicdone.add (2,NewStateObject () {key ="2", state =false }); Dicdone.add (3,NewStateObject () {key ="3", state =false }); Addevent Add= (A, b) = = { intR= A +b; Bags. ADD (R); returnR; }; Add. BeginInvoke (1,2, Callback, dicdone[1]); Add= (A, b) = = { intr = A +b; Bags. ADD (R); returnR; }; Add. BeginInvoke (3,4, Callback, dicdone[2]); Add= (A, b) = ={Thread.Sleep (10000); intr = A +b; Bags. ADD (R); returnR; }; Add. BeginInvoke (5,6, Callback, dicdone[3]); Thread th=NewThread ((o) = { while(true) { intCX =DicDone.Keys.Count; intx =0; foreach(varIteminchDicdone) { if(item. Value.state = =true) {x++; } } if(x = =CX) {t.set (); Console.WriteLine ("Resutl is:"); foreach(varIteminchbags) {Console.WriteLine (item); } Break; } } }); Th. Start (); T.waitone (); Console.WriteLine ("Queue complete!! "); //sockettest.send ("127.0.0.1", 11000, "www.jb51.net");Console.read (); } Private Static voidcallback (IAsyncResult ar) {varHandler =( addevent) ((AsyncResult) AR). AsyncDelegate; varState = Ar. AsyncState asStateObject; State.state=true; Console.WriteLine (State.key+"Thread Release!! "); Console.WriteLine (AR. asyncstate); }}
C # multithreaded data distribution loading