• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#多线程数据分布加载

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
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("\nPress 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.ogeek.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);
    }
}

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#操作txt问件,进行清空添加操作发布时间:2022-07-14
下一篇:
C#解析HTML发布时间:2022-07-14
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap