internal void Start() { isStart = true; Task.Run(() => { while (isStart) { if (RunningTasks.Count < MaxTaskCount) { DataInfo commandinfo; if (commandInfos.TryDequeue(out commandinfo)) { Task task = new Task(() => { var command = DbHelper.Current.Get <PoolCommand>(DataType.CommandType, commandinfo.ID); HeartbeatCommand.UpdateHeartTime(commandinfo.State); PoolJob.TcpServer.ReceivedCommandAction(commandinfo.State, command); }); task.ContinueWith(t => { RunningTasks.Remove(task); }); RunningTasks.Add(task); task.Start(); } } } }); }
public void 繞行SafeCollection操作集合_不同執行緒同時對SafeCollection新增與刪除元素_不應擲出例外() { var safeCollection = new SafeCollection <int> { 1, 2, 3, 4, 5 }; Task.Run(() => { while (true) { safeCollection.Add(0); } }); Task.Run(() => { while (true) { safeCollection.Remove(0); } }); Task.Run(() => { while (true) { safeCollection.Clear(); } }); Action action = () => IterateCollection(safeCollection).Wait(); action.Should().NotThrow(); }
public void 初始化一個SafeCollection並加入1個元素_加入成功_集合中應包含該元素且長度為1() { var safeCollection = new SafeCollection <int>(); var fixture = new Fixture(); var element = fixture.Create <int>(); safeCollection.Add(element); const int expecetedCount = 1; safeCollection.First().Should().Be(element); safeCollection.Count.Should().Be(expecetedCount); }
public void Regist(string key, Action <string> action) { if (msgs.ContainsKey(key)) { msgs[key].Add(action); } else { SafeCollection <Action <string> > actions = new SafeCollection <Action <string> >(); actions.Add(action); msgs.Add(key, actions); } }
public void Add(Task item) { if (item == null) { return; } TaskInfo taskInfo = new TaskInfo() { Status = TaskStatus.Waiting, Task = item }; tasks.Add(taskInfo); }
public void Add(Type type, MemberMetadata metadata) { type.ThrowIfNull("type", "Parameter cannot be null."); if (_container.ContainsKey(type)) { _container[type].Add(metadata); } else { SafeCollection<MemberMetadata> holder = new SafeCollection<MemberMetadata>(); holder.Add(metadata); _container.TryAdd(type, holder); } }
public void Add(Type contract, FastReflection.IDynamicConstructor ctor) { contract.ThrowIfNull("contract", "Parameter cannot be null."); ctor.ThrowIfNull("ctor", "Parameter cannot be null."); if (_ctorContainer.ContainsKey(contract)) { _ctorContainer[contract].Add(ctor); } else { SafeCollection<FastReflection.IDynamicConstructor> collection = new SafeCollection<FastReflection.IDynamicConstructor>(); collection.Add(ctor); _ctorContainer.TryAdd(contract, collection); } }
public static void AddForgeMsg(string json) { try { var msg = JsonConvert.DeserializeObject <ForgeMsg>(json); if (msg == null) { return; } ForgeMsgs.Add(msg); } catch (Exception ex) { LogHelper.Error(ex.ToString()); } }
public void Add(Task item, string name = null) { if (item == null) { return; } //if (!string.IsNullOrEmpty(name)) //{ // LogHelper.Warn($"前排任务数{tasks.Count()} 当前任务:{name}"); //} TaskInfo taskInfo = new TaskInfo() { Status = TaskStatus.Waiting, Task = item }; tasks.Add(taskInfo); }
/// <summary> /// 发送通知 /// </summary> public bool SendNotice(Guid sendUserId, string sendNickName, Guid receiveUserId, int noticeType, int status, string args) { try { //InstanceContext instanceContext = new InstanceContext(new CommunicationCallback()); var noticeModel = new NoticeModel { SendUserId = sendUserId, SendNickName = sendNickName, ReceiveUserId = receiveUserId, NoticeType = noticeType, Status = status, Args = args, LastActiveDate = DateTime.Now }; //处理用户消息 SafeCollection <NoticeModel> listNoticeModel = new SafeCollection <NoticeModel>(); listNoticeModel.Add(noticeModel); ICommunicationCallback _client = new CommunicationCallback(); //向服务器发送通知 _client.SendNotice(listNoticeModel); //using (DuplexChannelFactory<ICommunication> channelFactory = new DuplexChannelFactory<ICommunication>(instanceContext, "CommunicationService")) //{ // ICommunication proxy = channelFactory.CreateChannel(); // using (proxy as IDisposable) // { // proxy.InsertNotice(noticeModel); // } //} return(true); } catch (Exception ex) { return(false); } }
public async void UpdateVisualization(Choosability.FixerBreaker.KnowledgeEngine.ThoughtProgress p) { await Task.Factory.StartNew(() => { if (p.BoardsAdded != null) { foreach (var b in p.BoardsAdded) { var bi = new BoardInfo() { Board = b }; bi.InitialLocation = NextLocation(); bi.Color = Color.FromArgb(255, 225, 0, 0); _boardInfo.Add(bi); } } if (p.BoardsRemoved != null) { foreach (var bi in _boardInfo) { if (p.BoardsRemoved.Contains(bi.Board)) { bi.IsRemoved = true; if (p.WinLength == 0) { bi.Color = Color.FromArgb(255, 0, 0, 255); } else { bi.Color = Color.FromArgb(255, 0, (byte)Math.Max(255 - p.WinLength * 25, 0), 0); } } } } }); Invalidate(); }
internal void Start() { isStart = true; Task.Run(() => { while (isStart) { if (RunningTasks.Count < MaxTaskCount) { Task command; if (loginCommands.TryDequeue(out command)) { command.ContinueWith(t => { RunningTasks.Remove(command); }); RunningTasks.Add(command); command.Start(); } } } }); }
/// <summary> /// 发送消息 /// </summary> public bool SendPrivateMessage(Guid sendUserId, Guid receiveUserId, string message) { try { //InstanceContext instanceContext = new InstanceContext(new CommunicationCallback()); var messageModel = new MessageModel { SendUserId = sendUserId, ReceiveUserId = receiveUserId, Message = message, SendTimeOfService = DateTime.Now }; //处理用户消息 SafeCollection <MessageModel> listMessageModel = new SafeCollection <MessageModel>(); listMessageModel.Add(messageModel); ICommunicationCallback _client = new CommunicationCallback(); //向服务器发送消息 _client.SendMessage(listMessageModel); //using (DuplexChannelFactory<ICommunication> channelFactory = new DuplexChannelFactory<ICommunication>(instanceContext, "CommunicationService")) //{ // ICommunication proxy = channelFactory.CreateChannel(); // using (proxy as IDisposable) // { // proxy.InsertMessage(message); // } //} return(true); } catch (Exception ex) { return(false); } }
public void TetsTypeGetHashCode() { SafeCollection<int> coll = new SafeCollection<int>(); var types = from a in AppDomain.CurrentDomain.GetAssemblies().AsParallel() let tps = a.GetTypes() from t in tps where !a.FullName.Contains("System") && t.IsPublic && t != null & !t.IsNested select new { Asm= a, Types= tps }; foreach(var item in types) { if (item.Asm == null) continue; foreach (Type t in item.Types) { if (t != null) { coll.Add(t.GetHashCode()); } } } }
internal void Start() { isStart = true; Task.Run(() => { while (isStart) { if (RunningTasks.Count < MaxTaskCount) { DataInfo analysisDataInfo; if (analysisDataIds.TryDequeue(out analysisDataInfo)) { Task task = new Task(() => { var analysisData = DbHelper.Current.Get <byte[]>(DataType.ReceiveType, analysisDataInfo.ID); var state = analysisDataInfo.State; var buffer = analysisData; var commandDataList = new List <byte[]>(); var index = 0; List <byte> bytes = null; while (index < buffer.Length) { if (bytes == null) { if ((index + 3) < buffer.Length && buffer[index] == PoolCommand.DefaultPrefixBytes[0] && buffer[index + 1] == PoolCommand.DefaultPrefixBytes[1] && buffer[index + 2] == PoolCommand.DefaultPrefixBytes[2] && buffer[index + 3] == PoolCommand.DefaultPrefixBytes[3]) { bytes = new List <byte>(); bytes.AddRange(PoolCommand.DefaultPrefixBytes); index += 4; } else { index++; } } else { if ((index + 3) < buffer.Length && buffer[index] == PoolCommand.DefaultSuffixBytes[0] && buffer[index + 1] == PoolCommand.DefaultSuffixBytes[1] && buffer[index + 2] == PoolCommand.DefaultSuffixBytes[2] && buffer[index + 3] == PoolCommand.DefaultSuffixBytes[3]) { bytes.AddRange(PoolCommand.DefaultSuffixBytes); commandDataList.Add(bytes.ToArray()); bytes = null; index += 4; } else { bytes.Add(buffer[index]); index++; } } } foreach (var data in commandDataList) { try { var cmd = PoolCommand.ConvertBytesToMessage(data); if (cmd != null) { MsgPool.Current.AddCommand(new CommandState { State = state, Command = cmd }); } } catch (Exception ex) { LogHelper.Error("Error occured on deserialize messgae: " + ex.Message, ex); } } }); task.ContinueWith(t => { RunningTasks.Remove(task); }); RunningTasks.Add(task); task.Start(); } } } }); }