namespace WCFLib { // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in both code and config file together. [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, ConcurrencyMode = ConcurrencyMode.Reentrant)] public class Publisher : IPublisher { private static readonly ConcurrentDictionary<iCallBack, Package> Clients = new ConcurrentDictionary<iCallBack, Package>(); public void Register() { var client = OperationContext.Current.GetCallbackChannel<iCallBack>(); if (!Clients.ContainsKey(client)) { Clients.TryAdd(client, new Package(DateTime.Now)); } } public void SendHeartbeatPackage() { var client = OperationContext.Current.GetCallbackChannel<iCallBack>(); if (Clients.ContainsKey(client)) { Clients[client] = new Package(DateTime.Now); } Console.WriteLine("Begin to update heartbeat package."); Console.WriteLine("Updated time is {0}", (Clients[client] as Package).Time); } } }Reentrant 模式下 并不是真正的并发吧? 改成multi的 你的Register 方法就不对了
呵呵你太小看现在的CPU速度了,
你可以自己测试一下,我相信每秒十万上下只是起步。
YCG_893,谢谢,我明白了,我不用担心性能问题了。还有个附加问题,你说的回发时使用多线程是不是指在server端当我callback每个客户端时都使用独立的线程来完成针对每个客户端的调用?
我在server端有个方法实现的就是遍历所有客户端,然后给所有客户端发命令,代码如下 public void KickoffTest()
{
foreach (var item in Clients)
{
//我该在这里用多线程来实现么?
item.Key.SendRequestToClient("Begin Testing");
}
}
不是在这里发命令时候要用多线程去实现?
lock(lockObject)
{
ClientKeys = Clients.Keys.ToArray();
}
foreach (var item in ClientKeys)
{
//新线程或加入线程池
item.SendRequestToClient("Begin Testing");
}
namespace WCFLib
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in both code and config file together.
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, ConcurrencyMode = ConcurrencyMode.Reentrant)]
public class Publisher : IPublisher
{
private static readonly ConcurrentDictionary<iCallBack, Package> Clients = new ConcurrentDictionary<iCallBack, Package>();
public void Register()
{
var client = OperationContext.Current.GetCallbackChannel<iCallBack>();
if (!Clients.ContainsKey(client))
{
Clients.TryAdd(client, new Package(DateTime.Now));
} } public void SendHeartbeatPackage()
{
var client = OperationContext.Current.GetCallbackChannel<iCallBack>(); if (Clients.ContainsKey(client))
{
Clients[client] = new Package(DateTime.Now);
} Console.WriteLine("Begin to update heartbeat package.");
Console.WriteLine("Updated time is {0}", (Clients[client] as Package).Time);
} }
}Reentrant 模式下 并不是真正的并发吧? 改成multi的 你的Register 方法就不对了
不会的 只要不是
while(true){
//心跳包发送
}
就不会出问题另外一分钟发一个也没有问题