• C#多线程数据分布加载


    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);
        }
    }
  • 相关阅读:
    代码改变世界,随手写了点代码解决了一个小学生级别的作业题,编程要从娃娃抓起
    智和信通实现信创国产化适配 助力信创生态智能运维体系建设
    智和信通赋能国产信创 建设IT智能监控运维体系
    北京智和信通智慧档案馆网络监控运维解决方案
    图书馆网络运维监控安全态势感知方案-智和信通
    广播电视网络运维安全态势感知解决方案
    北京智和信通荣膺2020智能运维星耀榜最具影响力企业
    直播新规出台!如何用技术管住乱打赏的「熊孩子」?
    是什么让你在赛博空间更好看?
    Niu Talk 数据科学系列论坛:明晚,我们聊聊大数据与开源
  • 原文地址:https://www.cnblogs.com/kexb/p/6903296.html
Copyright © 2020-2023  润新知