public void RequestCancellation() { _state = CancellationState.CancellationRequested; }
/// <summary> /// 刷新工具栏 /// </summary> protected virtual void RefreshTool() { switch (BillState) { case State.None: this.toolStripNew.Enabled = true; this.toolStripDelete.Enabled = false; this.toolStripEdit.Enabled = false; this.toolStripCancel.Enabled = false; this.toolStripSave.Enabled = false; this.toolStripApprove.Enabled = false; this.toolStripReApprove.Enabled = false; this.toolStripPrint.Enabled = false; this.toolStripExport.Enabled = false; this.toolStripCancellation.Enabled = false; this.picFlag.Image = null; break; case State.New: goto case State.Edit; case State.Edit: this.toolStripNew.Enabled = false; this.toolStripDelete.Enabled = false; this.toolStripEdit.Enabled = false; this.toolStripCancel.Enabled = true; this.toolStripSave.Enabled = true; this.toolStripApprove.Enabled = false; this.toolStripReApprove.Enabled = false; this.toolStripPrint.Enabled = false; this.toolStripExport.Enabled = false; this.toolStripCancellation.Enabled = false; if (billState == State.New) { this.picFlag.Image = Resource1.Adding; } else { this.picFlag.Image = Resource1.Editing; } break; case State.Query: bool canEdit = CanEdit(); this.toolStripNew.Enabled = true; this.toolStripDelete.Enabled = CanDelete(); this.toolStripEdit.Enabled = canEdit; this.toolStripCancel.Enabled = false; this.toolStripSave.Enabled = false; ApproveState approveState = IsApprove(); CancellationState cancellationState = IsCancelled(); this.picFlag.Image = Resource1.Saved; switch (approveState) { case ApproveState.No: this.toolStripApprove.Enabled = true; this.toolStripReApprove.Enabled = false; break; case ApproveState.Yes: this.toolStripApprove.Enabled = false; this.toolStripReApprove.Enabled = true; this.picFlag.Image = Resource1.Approved; break; case ApproveState.Error: this.toolStripApprove.Enabled = false; this.toolStripReApprove.Enabled = false; break; } this.toolStripPrint.Enabled = true; this.toolStripExport.Enabled = true; switch (cancellationState) { case CancellationState.No: //如果单据未审核并且可以修改 才能被取消,否则不能再取消 if (approveState != ApproveState.Yes && canEdit) { this.toolStripCancellation.Enabled = true; } else { this.toolStripCancellation.Enabled = false; } break; case CancellationState.Yes: //取消了不能再修改,也不能再做其它操作 this.toolStripNew.Enabled = true; this.toolStripDelete.Enabled = false; this.toolStripEdit.Enabled = false; this.toolStripCancel.Enabled = false; this.toolStripSave.Enabled = false; this.toolStripApprove.Enabled = false; this.toolStripReApprove.Enabled = false; this.toolStripPrint.Enabled = false; this.toolStripExport.Enabled = false; this.toolStripCancellation.Enabled = false; this.picFlag.Image = Resource1.Cancelled; break; case CancellationState.Error: this.toolStripCancellation.Enabled = false; break; } break; } }
private static bool ProducerConsumer_InParallel <T>(ElementFactory <T> factory, int dataSize, int streamCount, int bufferCapacity, int chunkSize) { TestHarness.TestLog("ProducerConsumer_InParallel<{0}>: {1}, {2}, {3}, {4}", typeof(T).Name, dataSize, streamCount, bufferCapacity, chunkSize); ManualResetEvent m_startEvent = new ManualResetEvent(false); // Create the channels. TestHarness.TestLog(" > Creating {0} channels w/ capacity of {1}", streamCount, bufferCapacity); AsynchronousChannel <T>[] channels = new AsynchronousChannel <T> [streamCount]; for (int i = 0; i < channels.Length; i++) { channels[i] = new AsynchronousChannel <T>(bufferCapacity, chunkSize, new CancellationToken()); } // Create N threads to produce data. for (int i = 0; i < streamCount; i++) { int idx = i; ThreadPool.QueueUserWorkItem(delegate { m_startEvent.WaitOne(); TestHarness.TestLog(" > Producer #{0}: creating {1} data elements on thread {2}", idx, dataSize, Thread.CurrentThread.ManagedThreadId); for (int j = 0; j < dataSize; j++) { channels[idx].Enqueue(factory(j)); } TestHarness.TestLog(" > Producer #{0} exiting...", idx); channels[idx].SetDone(); }); } // Unleash the zombies. m_startEvent.Set(); // And now consume the data on the current thread. CancellationState dummyCancellationState = new CancellationState( new CancellationTokenSource().Token); QueryTaskGroupState qtgs = new QueryTaskGroupState(dummyCancellationState, 0); qtgs.QueryBegin(Task.Factory.StartNew(delegate { for (int i = 0; i < channels.Length; i++) { Task.Factory.StartNew(delegate { }); } })); AsynchronousChannelMergeEnumerator <T> e = new AsynchronousChannelMergeEnumerator <T>(qtgs, channels); int count = 0; TestHarness.TestLog(" > Consuming elements..."); while (e.MoveNext()) { //if ((count % (dataSize / 10)) == 0) TestHarness.TestLog(" {0} elems so far", count); T item = e.Current; count++; } foreach (AsynchronousChannel <T> c in channels) { Debug.Assert(c.IsDone && c.IsChunkBufferEmpty); } bool passed = (count == (streamCount * dataSize)); TestHarness.TestLog(" > Count is {0}, expected {1} ({2}*{3}): {4}", count, streamCount * dataSize, streamCount, dataSize, passed); return(passed); }