private async Task RunDispatcher() { int activeLoaderCount = 2; while (true) { LockQueue(); foreach (var item in _inQueue) { _inWait.Add(item); } if (_inQueue.Count == 0) { _isDispatcherRuned = false; return; } UnLockQueue(); do { LockWait(); LockProccess(); for (int i = 0; i < activeLoaderCount && _inWait.Count > 0; i++) { _inProccess.Add(_inWait[0]); _inWait.RemoveAt(0); } if (_inProccess.Count == 0) { break; } UnLockProccess(); UnLockWait(); _mAc.AboutToBegin(activeLoaderCount); foreach (var item in _inProccess) { await item.Start().ContinueWith(task => _mAc.JustEnded()); } var token = new TaskCompletionSource <CoordinationStatus>(); await _mAc.AllBegun(token); foreach (var itemDone in _inProccess) { LockHistory(); _history.Add(itemDone); UnLockHistory(); } } while (true); } }
public MultiWebRequests(Int32 timeout = Timeout.Infinite) { var httpClient = new HttpClient(); foreach (var server in m_servers.Keys) { m_ac.AboutToBegin(1); httpClient.GetByteArrayAsync(server) .ContinueWith(task => ComputeResult(server, task)); } m_ac.AllBegun(AllDone, timeout); }
public MultiWebRequests(int timeout = Timeout.Infinite) { //以异步方式一次性发起所有请求 var httpClient = new HttpClient(); foreach (var server in m_servers.Keys) { m_ac.AboutToBegin(1); httpClient.GetByteArrayAsync(server).ContinueWith(task => ComputeResult(server, task)); } //告诉AsyncCoordinator所有操作都已发起,并在所有操作完成调用Cancel或者发生超时的时候调用AllDone m_ac.AllBegun(AllDone, timeout); }
public MultiWebRequests(Int32 timeout = Timeout.Infinite) { // Asynchronously initiate all the requests all at once var httpClient = new HttpClient(); foreach (string server in _servers.Keys) { _ac.AboutToBegin(); httpClient.GetByteArrayAsync(server).ContinueWith(task => ComputeResult(server, task)); } // Tell AsyncCoordinator that all operations have been initiated and to call // AllDone when all operations complete, Cancel is called, or the timeout occurs _ac.AllBegun(AllDone, timeout); }
private Object[] m_results; // Either a WebResponse or an Exception public MultiWebRequests(Int32 timeout = Timeout.Infinite) { // Create the response array: one response for each request m_results = new Object[m_requests.Length]; // Asynchronously initiate all the requests all at once for (Int32 n = 0; n < m_requests.Length; n++) { m_ac.AboutToBegin(1); m_requests[n].BeginGetResponse(EndGetResponse, n); } // Tell the helper class that all operations have been initiated // and to call AllDone when all operations complete, Cancel is // called, or the timeout occurs m_ac.AllBegun(AllDone, timeout); }
private void TimerCallBack() { // 如果已经连接就返回,此处采用原子操作实现 if (Interlocked.CompareExchange(ref ConnectStatus, 1, 0) == 0) { m_ac = new AsyncCoordinator(); for (int i = 0; i < PlcStates.Length; i++) { PlcStates[i].IsConnect = false; PlcStates[i].WorkSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); PlcStates[i].WorkSocket.BeginConnect(new IPEndPoint(PlcStates[i].PlcIpAddress, PlcStates[i].GetPort()), new AsyncCallback(PlcConnectCallBack), PlcStates[i]); PlcStates[i].LengthDataHead = 0; PlcStates[i].LengthDataContent = 0; m_ac.AboutToBegin(1); } // 启动检查连接情况 m_ac.AllBegun(AllDown); } }