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"] == "")
{
Console.WriteLine();
}
else if (dic["MessageType"] == "")
{
Console.WriteLine(); }
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, );
// 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();
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[]; while (true)
{
handler.BeginReceive(buff, , , , 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[]; }
}
}
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(, new StateObject() { key="",state=false });
dicDone.Add(, new StateObject() { key = "", state = false });
dicDone.Add(, new StateObject() { key = "", state = false }); AddEvent add = (a, b) =>
{
int r= a + b;
bags.Add(r); return r;
}; add.BeginInvoke(, , callback, dicDone[]); add = (a, b) =>
{
int r = a + b;
bags.Add(r); return r;
}; add.BeginInvoke(, , callback, dicDone[]); add = (a, b) =>
{
Thread.Sleep();
int r = a + b;
bags.Add(r); return r;
}; add.BeginInvoke(, , callback, dicDone[]);
Thread th = new Thread((o) =>
{ while (true)
{
int cx = dicDone.Keys.Count;
int x = ;
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);
}
}

C#多线程数据分布加载的更多相关文章

  1. Android 多线程 异步加载

    Android 应用中需要显示网络图片时,图片的加载过程较为耗时,因此加载过程使用线程池进行管理, 同时使用本地缓存保存图片(当来回滚动ListView时,调用缓存的图片),这样加载和显示图片较为友好 ...

  2. C# 多线程 异步加载 窗体

    C# 多线程 异步加载 窗体 分类: C#2014-05-28 16:57 1286人阅读 评论(0) 收藏 举报 异步加载 我们在使用 windowform 编程的时候,我们或许可能会越到,各种在窗 ...

  3. 多线程异步加载图片async_pictures

    异步加载图片 目标:在表格中异步加载网络图片 目的: 模拟 SDWebImage 基本功能实现 理解 SDWebImage 的底层实现机制 SDWebImage 是非常著名的网络图片处理框架,目前国内 ...

  4. JAVA多线程超时加载当网页图片

    先上图: 这一次没有采取正则匹配,而采取了最简单的java分割和替代方法进行筛选图片 它能够筛选如下的图片并保存到指定的文件夹 如: “http://xxxx/xxxx/xxx.jpg” 'http: ...

  5. ImageLoader(多线程网络图片加载)+本地缓存 for windowsphone 7

    搞了好长一阵子wp,做点好事. C/S手机app中应用最多的是  获取网络图片,缓存到本地,展示图片 本次主要对其中的delay:LowProfileImageLoader进行修改,在获取图片的时候, ...

  6. Android ListView避免多线程加载一个同一资源

    当我们的ListView中的Item包含图片,而且这些图片是同一资源,我们用多线程去加载图片,这时候可能就发生了这种情况. 比如线程是人,第一个人去做加载图片到缓存的工作,还没做好时第二个人要这同一张 ...

  7. 使用multiprocessing解决PyMuPDF不支持多线程加载导致的界面卡死无响应问题,及一个PyQt5实现的简易PDF阅读器例子

    最近在用PyMuPDF实现一个PDF阅读器,发现PyMuPDF在加载某些epub时耗时非常长,有的长达10几秒,会导致界面卡死无响应. 尝试用多线程后台加载,发现还是不能解决问题,和作者交流(issu ...

  8. android ImageLoader 混淆加载drawable出现黑色图片的解决方案

    在网上找了很久,没有找到.后来看了源码才知道... 多线程异步加载和显示图片(图片来源于网络.sd卡.assets文件夹,drawable文件夹(不能加载9patch),新增加载视频缩略图) Stri ...

  9. ImageLoader图片加载

    http://blog.csdn.net/liu1164316159/article/details/38728259       转载请注明http://write.blog.csdn.net/po ...

随机推荐

  1. Ext.js高级组件

    第二章:Ext.js高级组件 grid组件 普通方式 表格面板类Ext.grid.Panel xtype(别名):gridpanel.grid title标题.renderTo渲染至.width宽.h ...

  2. [BZOJ3162]独钓寒江雪

    bzoj description 你要给一个树上的每个点黑白染色,要求白点不相邻.求本质不同的染色方案数. 两种染色方案本质相同当且仅当对树重新标号后对应节点的颜色相同. \(n\le 5\times ...

  3. Web service 框架比较CXF xfire aisx2 aisx

    Web 服务框架.它还体现了从 Axis 1.x 系列获得的经验和最近两年在 Web 服务领域的发展.推出 Axis2 的主要原因之一是从速度和内存方面获得更好的性能——不过还添加了一些新特性和功能. ...

  4. ubuntu 16.04安装HUSTOJ过程

    一.背景介绍: 因为工作需要,想在学校搭建一个OJ平台用于程序测试与评价.于是需要搭建oJ,由于之前都是在云端服务器搭建系统,没有在实际服务器平台搭建过,所以遇到不少坑,都靠自己来填补.故而写下此教程 ...

  5. 学习OCI编程

    转自:http://kulong0105.blog.163.com/blog/static/174406191201162145944574/ 最近公司做的一个项目,要处理海量数据,数据是存放在Ora ...

  6. shred_linux_unix

    Sometimes you need to destroy or wipe data from hard drives (for example, before you sell your old h ...

  7. 视频支持拖动进度条播放的实现(基于nginx)

    http协议下的flv/mp4流式播放支持的三个要点: 1 服务器端要支持flv/mp4流式播放,现在nginx或者lighttpd都是支持这样的应用的,还支持mp4的流式播放(默认编译版本一般都是打 ...

  8. java之mapstruct的应用

    一.MapStruct是一个代码生成器,简化了不同的Java Bean之间映射的处理,所以映射指的就是从一个实体变化成一个实体.例如我们在实际开发中,DAO层的实体和一些数据传输对象(DTO),大部分 ...

  9. React性能优化 PureComponent

    为什么使用? React15.3中新加了一个 PureComponent 类,顾名思义, pure 是纯的意思, PureComponent 也就是纯组件,取代其前身 PureRenderMixin  ...

  10. 符合RESTful规范的API

    统一使用的utils,serializers: class BaseResponse: def __init__(self): self.code = 1000 self.data = None se ...