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. 堆排序(C语言实现)

    一.堆的概念 所谓堆,它是一个数组,也能够被看成一个近似的全然二叉树.树上每一个结点相应数组的一个元素.二叉堆分为二种:最大堆和最小堆.本文主要介绍最大堆,最小堆类似.最大堆的特点:对于随意某个结点, ...

  2. 自动下载google reader里面的星标文章

    1. google reader马上就要关闭了,最后一次看看俺的浏览记录吧 最近 30 天的统计信息 全部订阅: 367 已读条目: 151 已点击的条目:41 个 加星标条目: 16 已发电子邮件条 ...

  3. sorting--codility

    lesson 6: sorting 1. Distinct 2. Triangle 2. MaxProductOfThree 4. NumberOfDiscIntersections lesson 6 ...

  4. java工具类-读配置文件

    ///读配置文件 import java.io.InputStream;import java.util.HashMap;import java.util.Map;import java.util.M ...

  5. eclipse 和 Myeclipse中Maven Web项目出现小红叉的 详细解决方法

    在我们创建maven项目是常会出现小红叉,如图: 解决办法: 1.可以 点击鼠标右键  maven--->update project.一般可以解决. 2.查看 window---->sh ...

  6. [持续更新]一些zyys的题的集合

    Luogu P1119 灾后重建 Sol:对于每个中转点K,需且仅需以此松弛一次 Key words:Floyd,本质活用 考题 路径数 题目描述: Euphemia到一个N*N的药草田里采药,她从左 ...

  7. JavaWeb的过滤器Filter

    Filter的作用是在请求到达web资源(HTML.css.Servlet.Jsp)之前拦截,做一个预处理. 创建一个类实现Filter接口,在web.xml中对文件进行配置 <filter&g ...

  8. ubuntu安装tushare

    sudo apt-get install python-pandas sudo pip install tushare

  9. Controlling DNS prefetching

    Controlling DNS prefetching IN THIS ARTICLE Background Configuring prefetching in the browser Contro ...

  10. 关于HDU 5952的那些事

    内容过后再贴,先发表一下心情和感悟. 这个题,我TLE了十多发,后来看了别人的题解,思路是一样的,他做了剪枝的我也做了,为何他的能过的我的超时?后来发现一个不是主要问题的问题:大家的图存储用的都是前向 ...