protected IEnumerable <T> _GetObjectSet() { Operation operation; while (_Operations.TryDequeue(out operation)) { if (operation.Type == TYPE.ADD) { lock (_Items) { _Items.Add(operation.Item); } operation.Item.Launch(); if (AddEvent != null) { AddEvent(operation.Item); } } else { lock (_Items) _Items.Remove(operation.Item); operation.Item.Shutdown(); if (RemoveEvent != null) { RemoveEvent(operation.Item); } } } return(_Objects); }
public virtual ArraySegment <byte> FrameSerialize(DataFrame data, object body) { byte[] result; if (!mBuffers.TryDequeue(out result)) { result = new byte[this.Options.MaxBodyLength]; } string value; if (body is string) { value = (string)body; int length = Options.Encoding.GetBytes(value, 0, value.Length, result, 0); return(new ArraySegment <byte>(result, 0, length)); } else { value = Newtonsoft.Json.JsonConvert.SerializeObject(body); int length = Options.Encoding.GetBytes(value, 0, value.Length, result, 0); return(new ArraySegment <byte>(result, 0, length)); //System.IO.MemoryStream stream = new MemoryStream(result); //stream.SetLength(0); //stream.Position = 0; //var task = SpanJson.JsonSerializer.NonGeneric.Utf8.SerializeAsync(body, stream).AsTask(); //task.Wait(); //return new ArraySegment<byte>(result, 0, (int)stream.Length); } }
/// <summary> /// This function runs in a thread and is responsible for retrieve each message from untreatedInbox and /// converting it a message object and store it in treatedInbox. /// </summary> private void CastingMessages() { while (true) { if (_untreatedInbox.Count == 0) { continue; } try { Message receivedMessage; byte[] receivedBytes; while (!_untreatedInbox.TryDequeue(out receivedBytes)) { } ; // Added a useless string instead of the message for now // TODO: Serialization of messages to better decoding _treatedInbox.Enqueue(new Message(Perfomative.Failure)); MainCluster.GotNewMessage.Invoke(null, null); } catch (Exception e) { Console.WriteLine(e); } } }
void RunEvents() { while (!KillEvents) { System.Threading.Thread.Sleep(20); try { Events Event; while (NewEvents.TryDequeue(out Event)) { switch (Event.What) { case Events.EventType.GotData: { if (OnData != null) { OnData.Invoke(); } } break; } } } catch (System.Exception ex) { if (System.Diagnostics.Debugger.IsAttached) { System.Diagnostics.Debugger.Break(); } } } }
private void Logger_DoWork(object sender, DoWorkEventArgs e) { while (true) { if (TestBenchLogQ.Count > 0) { this.Dispatcher.BeginInvoke((Action)(() => { String log = TestBenchLog.Text; string newLog = string.Empty; if (TestBenchLogQ.TryDequeue(out newLog)) { TestBenchLog.Text = newLog + log; } })); } if (TransmitterLogQ.Count > 0) { this.Dispatcher.BeginInvoke((Action)(() => { String log = DataLog.Text; string newLog = string.Empty; if (TransmitterLogQ.TryDequeue(out newLog)) { DataLog.Text = DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss") + "\t" + newLog + Environment.NewLine + log; } })); } } }
public void RefreshTraffic() { PortEvent e; if (port_events_queue.TryDequeue(out e)) { TrafficEvents.Add(e.ToString()); } }
/// <summary> /// /// </summary> public void Clear() { T v; while (!mq_high.IsEmpty) { mq_high.TryDequeue(out v); } while (!mq_low.IsEmpty) { mq_low.TryDequeue(out v); } }
public string Read() { lock (_lockObject) { string thing; var success = _buffer.TryDequeue(out thing); if (success) { return(thing); } else { return(""); } } }
/// <summary> /// Function that runs in a thread and tries to send all the messages present in sendInbox. /// </summary> private void ResponseMessages() { while (true) { if (_sendInbox.Count == 0) { continue; } MessageContainer messageContainer; while (!_sendInbox.TryDequeue(out messageContainer)) { //TODO: To send } } }
/// <summary> /// If you want to be responsible yourself for handling data points, instead of letting DataHandlers handle them, you can call this. /// /// Read datapoints will be dequeueud and not read by other users of this object. /// </summary> /// <returns>The data points.</returns> /// <param name="count">How many data points to read.</param> public DataPoint[] ReadDataPoints(int count) { if (eventQueue.Count < count) { throw new UnityException("Not enough data points! Check UnreadDataPointCount first."); } DataPoint[] dataPoints = new DataPoint[count]; for (int i = 0; i < count; i++) { bool dequeued = false; while (!dequeued) { DataPoint readPoint; dequeued = eventQueue.TryDequeue(out readPoint); dataPoints[i] = readPoint; } } return(dataPoints); }
public virtual ArraySegment <byte> FrameSerialize(DataFrame data, object body) { byte[] result; if (!mBuffers.TryDequeue(out result)) { result = new byte[this.Options.MaxBodyLength]; } string value; if (body is string) { value = (string)body; } else { value = Newtonsoft.Json.JsonConvert.SerializeObject(body); } int length = Options.Encoding.GetBytes(value, 0, value.Length, result, 0); return(new ArraySegment <byte>(result, 0, length)); }
void _worker_DoWork(object sender, DoWorkEventArgs e) { WorkerArgs args = (e.Argument as WorkerArgs); if (args == null) { return; } lock (_launchTimingLock) { _lastLaunchInitiatedUtc = DateTime.UtcNow; } _serverIndex = 0; System.Collections.Concurrent.ConcurrentQueue <LaunchItem> globalQueue = args.ConcurrentLaunchQueue; _serverTotal = globalQueue.Count; if (_serverTotal == 0) { return; } LaunchItem launchItem = null; var accountLaunchTimes = _gameSessionMap.GetLaunchAccountTimes(); while (globalQueue.TryDequeue(out launchItem)) { int threadDelayMs = ConfigSettings.GetConfigInt("ThreadGameLaunchDelayMs", 100); Thread.Sleep(threadDelayMs); new Thread((x) => { Thread.CurrentThread.IsBackground = true; launchGameFromItem(args, (LaunchItem)x, accountLaunchTimes); }).Start(launchItem); if (_worker.CancellationPending) { e.Cancel = true; return; } } }
public void SendPrintData() { int hPrinterPort = 0; try { bool isFirstRIP = false; _bSendExited = _bReadExited = false; bool bFileHeader = true; while (_cachList.Count < 2048 && !_bReadExited) //缓存一定数量才开始打印16M { Thread.Sleep(100); } while ((hPrinterPort = CoreInterface.Printer_Open()) == 0) { Thread.Sleep(100); } SPrinterSetting ssNew = this.m_IPrinterChange.GetAllParam().PrinterSetting; SJobSetting sjobseting = new SJobSetting(); if (UIFunctionOnOff.SupportPrintMode) { //todo } if (UIFunctionOnOff.SupportMediaMode) { //todo } LayoutSetting curLayoutSetting = new LayoutSetting(); int layoutIdx = 0; if (CoreInterface.LayoutIndex >= 0) { layoutIdx = CoreInterface.LayoutIndex; } if (NewLayoutFun.GetLayoutSetting(layoutIdx, ref curLayoutSetting)) { ssNew.layoutSetting = curLayoutSetting; } if (ssNew.sExtensionSetting.AutoRunAfterPrint) { ssNew.sBaseSetting.fYAddDistance = ssNew.sExtensionSetting.fRunDistanceAfterPrint; ssNew.sExtensionSetting.bEnableAnotherUvLight = ssNew.sExtensionSetting.fRunDistanceAfterPrint > 0; CoreInterface.SetPrinterSetting(ref ssNew, false); //打印结束后继续扫描一段距离生效 } //打印前设置JobSetting sjobseting.bReversePrint = ssNew.sBaseSetting.bReversePrint; CoreInterface.SetSJobSetting(ref sjobseting); LogWriter.WriteLog(new string[] { "[RIP]Printer open" }, true); int cbBytesRead = 0; isFirstRIP = true; while (true) { if (_cachList.Count > 0) { MyStruct data = new MyStruct(); if (!_cachList.TryDequeue(out data)) { continue; } byte[] chRequest = data.buf; cbBytesRead = data.buflen; #if ADD_HARDKEY { int subsize = 32; byte[] lastValue = chRequest; if (bFileHeader) { bFileHeader = false; lastValue = new byte[BUFSIZE + 8]; byte[] mjobhead = new byte[subsize]; byte[] retValue = new byte[subsize + Marshal.SizeOf(typeof(int))]; for (int j = 0; j < BYHXSoftLock.JOBHEADERSIZE / subsize; j++) { Buffer.BlockCopy(chRequest, j * mjobhead.Length, mjobhead, 0, mjobhead.Length); BYHX_SL_RetValue ret = BYHXSoftLock.CheckValidDateWithData(mjobhead, ref retValue); Buffer.BlockCopy(retValue, 0, lastValue, j * retValue.Length, retValue.Length); } Buffer.BlockCopy(chRequest, BYHXSoftLock.JOBHEADERSIZE, lastValue, BYHXSoftLock.JOBHEADERSIZE + 8, chRequest.Length - BYHXSoftLock.JOBHEADERSIZE); int sendBytes = CoreInterface.Printer_Send(hPrinterPort, lastValue, cbBytesRead + 8); Debug.Assert(sendBytes == cbBytesRead + 8); } else { CoreInterface.Printer_Send(hPrinterPort, chRequest, cbBytesRead); } } #else { if (isFirstRIP) { string strLog = ""; int nVersion = 0; byte bReversePrint = 0; byte nPrintLayerNum = 0; int printmodePerLayer = 0; isFirstRIP = false; if (cbBytesRead >= 84) { for (int i = 0; i < 84; i++) { strLog += Convert.ToString(chRequest[i], 2) + ","; } LogWriter.WriteLog(new string[] { "[RIP]" + strLog }, true); nVersion = BitConverter.ToInt32(chRequest, 4); if (nVersion == 4) { bReversePrint = chRequest[55]; //nPrintLayerNum = chRequest[56]; //printmodePerLayer = BitConverter.ToInt32(chRequest, 57); //PrinterSettingHelper.SetPropertyWhiteInkLayer( // ref m_IPrinterChange.GetAllParam().PrinterSetting, bReversePrint, // nPrintLayerNum, // (uint) printmodePerLayer); CoreInterface.SetPrinterSetting(ref m_IPrinterChange.GetAllParam() .PrinterSetting); //isSetWhiteFormPrt = true; sjobseting = new SJobSetting(); sjobseting.bReversePrint = bReversePrint == 1; CoreInterface.SetSJobSetting(ref sjobseting); } else if (nVersion == 6) { bReversePrint = chRequest[71]; sjobseting = new SJobSetting(); sjobseting.bReversePrint = bReversePrint == 1; CoreInterface.SetSJobSetting(ref sjobseting); } } } //#else CoreInterface.Printer_Send(hPrinterPort, chRequest, cbBytesRead); } #endif } if (_cachList.Count == 0 && _bReadExited) { LogWriter.WriteLog( new string[] { string.Format("[SendPrintData] CachList.Count = {0};bReadExited={1}", _cachList.Count, _bReadExited) }, true); break; } if (_cachList.Count == 0) { LogWriter.WriteLog( new string[] { string.Format("[SendPrintData] CachList.Count = {0};waittime={1}", _cachList.Count, 10) }, true); Thread.Sleep(10); } } CoreInterface.Printer_Close(hPrinterPort); _bSendExited = true; SExtensionSetting extensionSetting = ssNew.sExtensionSetting; if (extensionSetting.fRunDistanceAfterPrint > 0 && extensionSetting.BackBeforePrint) { while (true) { JetStatusEnum status = CoreInterface.GetBoardStatus(); if (status == JetStatusEnum.Ready) { int speed = ssNew.sMoveSetting.nYMoveSpeed; MoveDirectionEnum dir = MoveDirectionEnum.Up; int len = Convert.ToInt32(extensionSetting.fRunDistanceAfterPrint * m_IPrinterChange.GetAllParam().PrinterProperty.fPulsePerInchY); CoreInterface.MoveCmd((int)dir, len, speed); break; } Thread.Sleep(100); } } } catch (Exception e) { LogWriter.WriteLog( new string[] { string.Format("[SendPrintData] CachList.Count = {0};Exception={1}", _cachList.Count, e.Message) }, true); if (hPrinterPort != 0) { CoreInterface.Printer_Close(hPrinterPort); _bSendExited = true; } } }