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. 7-17 Hashing(25 分)

    The task of this problem is simple: insert a sequence of distinct positive integers into a hash tabl ...

  2. 2018-2019-2 《网络对抗技术》Exp5 msf 20165222

    本实践目标是掌握metasploit的基本应用方式,重点常用的三种攻击方式的思路.具体需要完成: 总计:主动:ms17_010永恒之蓝(成功): 浏览器:office_word_hta(成功): 客户 ...

  3. 《DSP using MATLAB》Problem 2.2

    1.代码: %% ------------------------------------------------------------------------ %% Output Info abo ...

  4. StreamSets Data Collector Edge 说明

    Data Collector Edge 是不包含界面的agent 安装 下载包 https://streamsets.com/opensource tar xf streamsets-datacoll ...

  5. macOS --- 配置基于域名的虚拟主机

    在终端运行 sudo vi /Applications/XAMPP/xamppfiles/etc/httpd.conf,打开apache配置文件. 在httpd.conf中找到"#Inclu ...

  6. .NET泛型解析(下)

    上一篇对.NET中的泛型进行了详细的介绍以及使用泛型的好处是什么,这篇将更加深入的去了解泛型的其他的知识点,重头戏. [1]泛型方法 上一篇我们也说过了,泛型可以是类,结构,接口,在这些泛型类型中定义 ...

  7. 【解决Jira】Chrome提示Java插件因过期而遭到阻止(JIRA上传截屏截图)

    最近经常被这个问题所困扰:用Chrome访问JIRA上传截屏截图时,地址栏下面弹出通知,提示JAVA插件已过期.但是由于公司要求统一开发环境和设置,不能更新到最新版,就像这样: 结果网页上的Java就 ...

  8. VS2017开发Linux平台上的程序

    重装系统后安装VS2015时卡住了,于是试试看VS2017怎样,听说还支持调Linux.发现VS2017跟12/13/15又有了新的飞跃,竟然支持模块化下载,对于我这种主要写C++简直是个福音,勾了L ...

  9. URL 收录

    http://www.cnblogs.com/Olive116/p/3426957.html 地图控件 http://www.cnblogs.com/tugenhua0707/ 前端

  10. 《Linux内核精髓:精通Linux内核必会的75个绝技》一HACK #12 使用Memory Cgroup限制内存使用量

    HACK #12 使用Memory Cgroup限制内存使用量 Memory Cgroup是Cgroup的资源限制功能之一,可以控制特定进程可以使用的内存量.Memory CgroupMemory C ...