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. WinForm获取当前路径汇总

    Winform获取应用程序的当前路径的方法集合汇总,值得收藏备用 具体如下, //获取当前进程的完整路径,包含文件名(进程名). string str = this.GetType().Assembl ...

  2. 10件在PHP7中不要做的事情

    10件在PHP7中不要做的事情 1. 不要使用mysql_函数 这一天终于来了,从此你不仅仅“不应该”使用mysql_函数.PHP 7 已经把它们从核心中全部移除了,也就是说你需要迁移到好得多的mys ...

  3. [转]无网络环境,在Windows Server 2008 R2和SQL Server 2008R2环境安装SharePoint2013 RT

    无网络环境,在Windows Server 2008 R2和SQL Server 2008R2环境安装SharePoint2013 RT,这个还有点麻烦,所以记录一下,下次遇到省得绕弯路.进入正题: ...

  4. C BIN加密

    #include <stdio.h> #include <string.h> #include <stdlib.h> #ifndef DWORD #define D ...

  5. XSS 与 CSRF 两种跨站攻击

    在前几年,大家一般用拼接字符串的方式来构造动态 SQL 语句创建应用,于是 SQL 注入成了很流行的攻击方式, 但是现在参数化查询 已经成了普遍用法,我们已经离 SQL 注入很远了.但是历史同样悠久的 ...

  6. 黄聪:C#多线程教程(1):BeginInvoke和EndInvoke方法,解决主线程延时Thread.sleep柱塞问题(转)

    开发语言:C#3.0 IDE:Visual Studio 2008 本系列教程主要包括如下内容: 1.  BeginInvoke和EndInvoke方法 2.  Thread类 3. 线程池 4. 线 ...

  7. Devexpres下窗体带阴影的边框效果

    public partial class Form1 : DevExpress.XtraEditors.XtraForm { public Form1() { InitializeComponent( ...

  8. Java 获取字符串指定下标位置的值 charAt()

    Java手册 charAt public char charAt(int index) 返回指定索引处的 char 值.索引范围为从 0 到 length() - 1.序列的第一个 char 值位于索 ...

  9. mysql java.sql.SQLException: Can't call commit when autocommit=true

    java.sql.SQLException: Can't call commit when autocommit=true at com.mysql.jdbc.SQLError.createSQLEx ...

  10. myeclipse通过数据表生成jpa或hibernate实体

    1. 创建数据库连接 2. 选择表 3. 生成 hibernate mapping 4. 生产jpa