using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using SocketIM; using System.Net; using System.Net.Sockets; using ConsoleApplication1; using System.Runtime.Remoting.Messaging; using System.Threading; namespace ConsoleApplication2 { public interface IIMCallBack { void IMCallBack(byte[] data); } public class ServerHandler : IIMCallBack { public void IMCallBack(byte[] data) { string str = System.Text.Encoding.UTF8.GetString(data); var dic = 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 = new System.Threading.Thread(StartListening); thread.IsBackground = true; thread.Start(); } public static void StartListening() { //byte[] bytes = new Byte[1024]; IPAddress ipAddress = IPAddress.Parse("127.0.0.1"); IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000); // Create a TCP/IP socket. Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); // Bind the socket to the local //endpoint and listen for incoming connections. try { listener.Bind(localEndPoint); listener.Listen(100); while (true) { listener.BeginAccept(new AsyncCallback(AcceptCallback), listener); } } catch (Exception e) { Console.WriteLine(e.ToString()); } Console.WriteLine(" Press ENTER to continue..."); Console.Read(); } public static void AcceptCallback(IAsyncResult ar) { Socket listener = (Socket)ar.AsyncState; Socket handler = listener.EndAccept(ar); byte[] buff = new byte[1024]; while (true) { handler.BeginReceive(buff, 0, 1024, 0, new AsyncCallback(ReadCallback),handler); } } public static void ReadCallback(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. int bytesRead = state.EndReceive(ar); byte[] buff = new byte[1024]; } } } public delegate int AddEvent(int a, int b); class Program { public class StateObject { public string key { get; set; } public bool state { get; set; } } public static List<int> bags = new List<int>(); static void Main(string[] args) { ManualResetEvent t = new ManualResetEvent(false); Dictionary<int, StateObject> dicDone = new Dictionary<int, StateObject>(); dicDone.Add(1, new StateObject() { key="1",state=false }); dicDone.Add(2, new StateObject() { key = "2", state = false }); dicDone.Add(3, new StateObject() { key = "3", state = false }); AddEvent add = (a, b) => { int r= a + b; bags.Add(r); return r; }; add.BeginInvoke(1, 2, callback, dicDone[1]); add = (a, b) => { int r = a + b; bags.Add(r); return r; }; add.BeginInvoke(3, 4, callback, dicDone[2]); add = (a, b) => { Thread.Sleep(10000); int r = a + b; bags.Add(r); return r; }; add.BeginInvoke(5, 6, callback, dicDone[3]); Thread th = new Thread((o) => { while (true) { int cx = dicDone.Keys.Count; int x = 0; foreach (var item in dicDone) { if (item.Value.state == true) { x++; } } if (x == cx) { t.Set(); Console.WriteLine("resutl is:"); foreach (var item in bags) { Console.WriteLine(item); } break; } } }); th.Start(); t.WaitOne(); Console.WriteLine("队列完成!!"); //SocketTest.Send("127.0.0.1", 11000, "www.jb51.net"); Console.Read(); } private static void callback(IAsyncResult ar) { var handler = (AddEvent)((AsyncResult)ar).AsyncDelegate; var state = ar.AsyncState as StateObject; state.state = true; Console.WriteLine(state.key+"线程放行!!"); Console.WriteLine(ar.AsyncState); } }