表示可由多个线程同时访问的键/值对的线程安全集合。 命名空间: System.Collections.Concurrent 程序集: mscorlib(位于 mscorlib.dll) 继承层次结构 System.Object System.Collections.Concurrent.ConcurrentDictionary<TKey, TValue> 语法 C#
1 2 3 4 5 6 7 8 |
[SerializableAttribute] [ComVisibleAttribute(false)] [HostProtectionAttribute(SecurityAction.LinkDemand, Synchronization = true, ExternalThreading = true)] public class ConcurrentDictionary<TKey, TValue> : IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IEnumerable, IDictionary, ICollection, IReadOnlyDictionary<TKey, TValue>, IReadOnlyCollection<KeyValuePair<TKey, TValue>> |
类型参数 TKey 字典中的键的类型。 TValue 字典中的值的类型。 构造函数 名称 说明 ConcurrentDictionary<TKey, TValue>() 初始化 ConcurrentDictionary<TKey, TValue> 类的新实例,该实例为空,具有默认的并发级别和默认的初始容量,并为键类型使用默认比较器。 ConcurrentDictionary<TKey, TValue>(IEnumerable<KeyValuePair<TKey, TValue>>) 初始化 ConcurrentDictionary<TKey, TValue> 类的新实例,该实例包含从指定的 IEnumerable<T> 中复制的元素,具有默认的并发级别和默认的初始容量,并为键类型使用默认比较器。 ConcurrentDictionary<TKey, TValue>(IEnumerable<KeyValuePair<TKey, TValue>>, IEqualityComparer<TKey>) 初始化 ConcurrentDictionary<TKey, TValue> 类的新实例,该实例包含从指定的 IEnumerable 中复制的元素,具有默认的并发级别和默认的初始容量,并使用指定的 IEqualityComparer<T>。 ConcurrentDictionary<TKey, TValue>(IEqualityComparer<TKey>) 初始化 ConcurrentDictionary<TKey, TValue> 类的新实例,该实例为空,具有默认的并发级别和容量,并使用指定的System.Collections.Generic.IEqualityComparer<T>。 ConcurrentDictionary<TKey, TValue>(Int32, IEnumerable<KeyValuePair<TKey, TValue>>, IEqualityComparer<TKey>) 初始化 ConcurrentDictionary<TKey, TValue> 类的新实例,该实例包含从指定的 IEnumerable 中复制的元素并使用指定的System.Collections.Generic.IEqualityComparer<T>。 ConcurrentDictionary<TKey, TValue>(Int32, Int32) 初始化 ConcurrentDictionary<TKey, TValue> 类的新实例,该实例为空,具有指定的并发级别和容量,并为键类型使用默认比较器。 ConcurrentDictionary<TKey, TValue>(Int32, Int32, IEqualityComparer<TKey>) 初始化 System.Collections.Concurrent.ConcurrentDictionary<TKey, TValue> 类的新实例,该实例为空,具有指定的并发级别和指定的初始容量,并使用指定的 System.Collections.Generic.IEqualityComparer<T>。 属性 名称 说明 Count 获取包含在 System.Collections.Concurrent.ConcurrentDictionary<TKey, TValue> 中的键/值对的数目。 IsEmpty 获取一个值,该值指示 System.Collections.Concurrent.ConcurrentDictionary<TKey, TValue> 是否为空。 Item[TKey] 获取或设置与指定的键关联的值。 Keys 获得一个包含 System.Collections.Generic.Dictionary<TKey, TValue> 中的键的集合。 Values 获取包含 System.Collections.Generic.Dictionary<TKey, TValue> 中的值的集合。 方法 名称 说明 AddOrUpdate(TKey, TValue, Func<TKey, TValue, TValue>) 如果该键不存在,则将键/值对添加到 ConcurrentDictionary<TKey, TValue> 中;如果该键已经存在,则通过使用指定的函数更新 ConcurrentDictionary<TKey, TValue> 中的键/值对。 AddOrUpdate(TKey, Func<TKey, TValue>, Func<TKey, TValue, TValue>) 如果该键不存在,则使用指定函数将键/值对添加到 ConcurrentDictionary<TKey, TValue>;如果该键已存在,则使用该函数更新 ConcurrentDictionary<TKey, TValue>中的键/值对。 Clear() 将所有键和值从 ConcurrentDictionary<TKey, TValue> 中移除。 ContainsKey(TKey) 确定是否 ConcurrentDictionary<TKey, TValue> 包含指定键。 Equals(Object) 确定指定的对象是否等于当前对象。(继承自 Object。) Finalize() 在垃圾回收将某一对象回收前允许该对象尝试释放资源并执行其他清理操作。(继承自 Object。) GetEnumerator() 返回循环访问 ConcurrentDictionary<TKey, TValue> 的枚举数。 GetHashCode() 作为默认哈希函数。(继承自 Object。) GetOrAdd(TKey, TValue) 如果该键不存在,则将键/值对添加到 ConcurrentDictionary<TKey, TValue> 中。 GetOrAdd(TKey, Func<TKey, TValue>) 如果该键不存在,则通过使用指定的函数将键/值对添加到 ConcurrentDictionary<TKey, TValue> 中。 GetType() 获取当前实例的 Type。(继承自 Object。) MemberwiseClone() 创建当前 Object 的浅表副本。(继承自 Object。) ToArray() 将 System.Collections.Concurrent.ConcurrentDictionary<TKey, TValue> 中存储的键和值对复制到新数组中。 ToString() 返回表示当前对象的字符串。(继承自 Object。) TryAdd(TKey, TValue) 尝试将指定的键和值添加到 System.Collections.Concurrent.ConcurrentDictionary<TKey, TValue> 中。 TryGetValue(TKey, TValue) 尝试从 System.Collections.Concurrent.ConcurrentDictionary<TKey, TValue> 获取与指定的键关联的值。 TryRemove(TKey, TValue) 尝试从 System.Collections.Concurrent.ConcurrentDictionary<TKey, TValue> 中移除并返回具有指定键的值。 TryUpdate(TKey, TValue, TValue) 将指定键的现有值与指定值进行比较,如果相等,则用第三个值更新该键。 示例 C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
class CD_Ctor { // Demonstrates: // ConcurrentDictionary<TKey, TValue> ctor(concurrencyLevel, initialCapacity) // ConcurrentDictionary<TKey, TValue>[TKey] static void Main() { // We know how many items we want to insert into the ConcurrentDictionary. // So set the initial capacity to some prime number above that, to ensure that // the ConcurrentDictionary does not need to be resized while initializing it. int NUMITEMS = 64; int initialCapacity = 101; // The higher the concurrencyLevel, the higher the theoretical number of operations // that could be performed concurrently on the ConcurrentDictionary. However, global // operations like resizing the dictionary take longer as the concurrencyLevel rises. // For the purposes of this example, we'll compromise at numCores * 2. int numProcs = Environment.ProcessorCount; int concurrencyLevel = numProcs * 2; // Construct the dictionary with the desired concurrencyLevel and initialCapacity ConcurrentDictionary<int, int> cd = new ConcurrentDictionary<int, int>(concurrencyLevel, initialCapacity); // Initialize the dictionary for (int i = 0; i < NUMITEMS; i++) cd[i] = i * i; Console.WriteLine("The square of 23 is {0} (should be {1})", cd[23], 23 * 23); } } |
[…]
View DetailsSignalR 分:PersistentConnection和Hub 2种模式。 跨域又分:UseCors和JsonP 2种方法 所以例子写了4种。 核心代码: UseCors
1 2 3 4 5 6 7 8 9 10 11 12 |
//PersistentConnection Mode app.UseCors(CorsOptions.AllowAll); //PersistentConnection Mode app.MapSignalR<MyConnection>("/server", new ConnectionConfiguration() { }); // 有关如何配置应用程序的详细信息,请访问 http://go.microsoft.com/fwlink/?LinkID=316888 //Hub Mode app.MapSignalR("/lcc", new HubConfiguration()); |
Use JsonP
1 2 3 4 5 6 7 8 9 |
//PersistentConnection Mode app.MapSignalR<MyConnection>("/server", new ConnectionConfiguration() { EnableJSONP = true }); // 有关如何配置应用程序的详细信息,请访问 http://go.microsoft.com/fwlink/?LinkID=316888 //Hub Mode app.MapSignalR("/lcc", new HubConfiguration() { EnableJSONP = true }); |
Cross HUb 前端
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
// 建立对应server端Hub class的对象,请注意geffChat的第一个字母要改成小写 var chat = $.connection.myHub; $.connection.hub.url = 'http://localhost:2001/lcc'; //把connection打开 $.connection.hub.start().done(function () { chat.server.hello(); }); // 定义client端的javascript function,供server端hub,通过dynamic的方式,调用所有Clients的javascript function chat.client.sendMessage = function (message) { //当server端调用sendMessage时,将server push的message数据,呈现在wholeMessage中 $('#messages').append('<li>' + message + '</li>'); }; $("#broadcast").click(function () { chat.server.sendMessage($('#msg').val()); $('#msg').val(""); }); $("#btnStop").click(function () { $.connection.hub.stop(); }); |
Cross Connection 前端
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
var connection = $.connection("http://localhost:2001/server"); connection.received(function (data) { $('#messages').append('<li>' + data + '</li>'); }); connection.start(); $("#broadcast").click(function () { connection.send($('#msg').val()); }); $("#btnStop").click(function () { connection.stop(); }); |
JsonP HUb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
// 建立对应server端Hub class的对象,请注意geffChat的第一个字母要改成小写 var chat = $.connection.myHub; $.connection.hub.url = 'http://localhost:16727/lcc'; //把connection打开 $.connection.hub.start({ jsonp: true }).done(function () { chat.server.hello(); }); // 定义client端的javascript function,供server端hub,通过dynamic的方式,调用所有Clients的javascript function chat.client.sendMessage = function (message) { //当server端调用sendMessage时,将server push的message数据,呈现在wholeMessage中 $('#messages').append('<li>' + message + '</li>'); }; $("#broadcast").click(function () { chat.server.sendMessage($('#msg').val()); $('#msg').val(""); }); $("#btnStop").click(function () { $.connection.hub.stop(); }); |
JsonP connection
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
var connection = $.connection("http://localhost:16727/server"); connection.received(function (data) { $('#messages').append('<li>' + data + '</li>'); }); connection.start({ jsonp: true }); $("#broadcast").click(function () { connection.send($('#msg').val()); }); $("#btnStop").click(function () { connection.stop(); }); |
代码下载:http://download.csdn.net/detail/shikyoh/9734460 from: https://www.cnblogs.com/shikyoh/p/6272679.html
View Details