public static void TestAddingSubscribersToWorkerThread() { using (WorkerGroup workerGroup = new WorkerGroup()) { IThreadWorker worker = workerGroup.CreateWorker(); bool wasPrepared = false; worker.Prepare += (object sender, ThreadWorkerEventArgs e) => { wasPrepared = true; }; bool didWork = false; worker.Work += (object sender, ThreadWorkerEventArgs e) => { didWork = true; }; bool didComplete = false; worker.Completing += (object sender, ThreadWorkerEventArgs e) => { didComplete = true; }; worker.Run(); workerGroup.WaitAllAndFinish(); Assert.That(wasPrepared, "The Prepare event should be raised."); Assert.That(didWork, "The Work event should be raised."); Assert.That(didComplete, "The Completed event should be raised."); } }
public static void TestRemovingSubscribersFromWorkerThread() { using (WorkerGroup workerGroup = new WorkerGroup()) { IThreadWorker worker = workerGroup.CreateWorker(); worker.Prepare += ThreadWorkerEventHandler; worker.Work += ThreadWorkerEventHandler; worker.Completing += ThreadWorkerEventHandler; worker.Run(); workerGroup.WaitAllAndFinish(); Assert.That(workerGroup.FirstError, Is.EqualTo(FileOperationStatus.UnspecifiedError), "The status should be set by one of the event handlers."); } using (WorkerGroup workerGroup = new WorkerGroup()) { IThreadWorker worker = workerGroup.CreateWorker(); worker.Prepare += ThreadWorkerEventHandler; worker.Work += ThreadWorkerEventHandler; worker.Completing += ThreadWorkerEventHandler; worker.Prepare -= ThreadWorkerEventHandler; worker.Work -= ThreadWorkerEventHandler; worker.Completing -= ThreadWorkerEventHandler; worker.Run(); workerGroup.WaitAllAndFinish(); Assert.That(workerGroup.FirstError, Is.EqualTo(FileOperationStatus.Success), "None of the event handlers should have been called, so the status should not have been set there."); } }
public static Worker rebuildWorker(Worker inWorker, List <string> excludeProperty) { Sex s = null; if (!isExclude(excludeProperty, "Sex")) { if (inWorker.Sex != null) { s = new Sex(inWorker.Sex.Id); s.Name = inWorker.Sex.Name; } } Worker w = new Worker(inWorker.Id, inWorker.TabN, inWorker.Fio, s, inWorker.IsActive); w.WorkDate = inWorker.WorkDate; w.ChildcareBegin = inWorker.ChildcareBegin; w.ChildcareEnd = inWorker.ChildcareEnd; w.IsTabu = inWorker.IsTabu; if (!w.IsActive) { w.DateZ = inWorker.DateZ; } w.BeginDate = inWorker.BeginDate; if (!isExclude(excludeProperty, "nomBodyPartSizes")) { IList <NomBodyPartSize> nomBodyPartSizes = new List <NomBodyPartSize>(); foreach (var item in inWorker.NomBodyPartSizes) { NomBodyPartSize nomBodyPartSize = rebuildNomBodyPartSize(item); nomBodyPartSizes.Add(nomBodyPartSize); } w.NomBodyPartSizes = nomBodyPartSizes; } if (!isExclude(excludeProperty, "WorkerCategory")) { if (inWorker.WorkerCategory != null) { if (!isExclude(excludeProperty, "WorkerCategory")) { WorkerCategory wc = new WorkerCategory(inWorker.WorkerCategory.Id, inWorker.WorkerCategory.Name); w.WorkerCategory = wc; } } } if (!isExclude(excludeProperty, "WorkerGroup")) { if (inWorker.WorkerGroup != null) { if (!isExclude(excludeProperty, "WorkerGroup")) { WorkerGroup wg = new WorkerGroup(inWorker.WorkerGroup.Id, inWorker.WorkerGroup.Name); w.WorkerGroup = wg; } } } return(w); }
/// <summary> /// Shutdown all workers of netty /// </summary> /// <returns></returns> public async Task ShutdownWorkers() { await WorkerGroup.ShutdownGracefullyAsync(); await BossGroup.ShutdownGracefullyAsync(); await ClusterWorkerGroup.ShutdownGracefullyAsync(); await ClusterBossGroup.ShutdownGracefullyAsync(); }
public static void TestDoubleFinished() { using (WorkerGroup workerGroup = new WorkerGroup()) { IThreadWorker worker = workerGroup.CreateWorker(nameof(TestDoubleFinished)); worker.Run(); workerGroup.WaitAllAndFinish(); Assert.Throws <InvalidOperationException>(() => { workerGroup.WaitAllAndFinish(); }); } }
public async Task Stop() { await Task.WhenAll(Sockets.Select(s => s.Disconnect())); await Channel.CloseAsync(); await BossGroup.ShutdownGracefullyAsync(); await WorkerGroup.ShutdownGracefullyAsync(); }
public static void TestDoubleDispose() { Assert.DoesNotThrow(() => { using (WorkerGroup workerGroup = new WorkerGroup()) { IThreadWorker worker = workerGroup.CreateWorker(nameof(TestDoubleDispose)); worker.Run(); workerGroup.Dispose(); } }); }
public static void TestCoreFunctionality() { int threadCount = 0; int maxCount = 0; using (WorkerGroup workerGroup = new WorkerGroup()) { object threadLock = new object(); IThreadWorker worker1 = workerGroup.CreateWorker(nameof(TestCoreFunctionality) + "1"); worker1.WorkAsync = (ThreadWorkerEventArgs e) => { lock (threadLock) { ++threadCount; if (threadCount > maxCount) { maxCount = threadCount; } } Thread.Sleep(new TimeSpan(0, 0, 0, 0, 100)); return(Task.FromResult <object>(null)); }; worker1.Completing += (object sender, ThreadWorkerEventArgs e) => { --threadCount; }; worker1.Run(); IThreadWorker worker2 = workerGroup.CreateWorker(nameof(TestCoreFunctionality) + "2"); worker2.WorkAsync = (ThreadWorkerEventArgs e) => { lock (threadLock) { ++threadCount; if (threadCount > maxCount) { maxCount = threadCount; } } Thread.Sleep(new TimeSpan(0, 0, 0, 0, 100)); return(Task.FromResult <object>(null)); }; worker2.Completing += (object sender, ThreadWorkerEventArgs e) => { --threadCount; }; worker2.Run(); workerGroup.WaitAllAndFinish(); Assert.That(maxCount, Is.EqualTo(1), "There should never be more than one thread active at one time."); } }
public static void TestObjectDisposed() { WorkerGroup workerGroup = new WorkerGroup(); IThreadWorker worker = workerGroup.CreateWorker(nameof(TestObjectDisposed)); worker.Run(); workerGroup.Dispose(); worker = null; Assert.Throws <ObjectDisposedException>(() => { worker = workerGroup.CreateWorker(nameof(TestObjectDisposed)); }, "A call to a method on a disposed object should raise ObjectDisposedException."); Assert.That(worker, Is.Null, "The worker should still be null, since the previous attempt to create should fail with an exception."); Assert.Throws <ObjectDisposedException>(() => { workerGroup.WaitAllAndFinish(); }, "A call to a method on a disposed object should raise ObjectDisposedException."); }
public static void TestInvalidOperationException() { using (WorkerGroup workerGroup = new WorkerGroup()) { IThreadWorker worker = workerGroup.CreateWorker(nameof(TestInvalidOperationException)); bool?f = null; Assert.Throws <InvalidOperationException>(() => { f = worker.HasCompleted; }); Assert.That(!f.HasValue, "No value should be set, since an exception should have occurred."); Assert.Throws <InvalidOperationException>(() => { worker.Join(); }); worker.Abort(); } }
public static void TestCoreFunctionality() { int threadCount = 0; int maxCount = 0; using (WorkerGroup workerGroup = new WorkerGroup()) { object threadLock = new object(); IThreadWorker worker1 = workerGroup.CreateWorker(); worker1.Work += (object sender, ThreadWorkerEventArgs e) => { lock (threadLock) { ++threadCount; if (threadCount > maxCount) { maxCount = threadCount; } } Thread.Sleep(100); }; worker1.Completing += (object sender, ThreadWorkerEventArgs e) => { --threadCount; }; worker1.Run(); IThreadWorker worker2 = workerGroup.CreateWorker(); worker2.Work += (object sender, ThreadWorkerEventArgs e) => { lock (threadLock) { ++threadCount; if (threadCount > maxCount) { maxCount = threadCount; } } Thread.Sleep(100); }; worker2.Completing += (object sender, ThreadWorkerEventArgs e) => { --threadCount; }; worker2.Run(); workerGroup.WaitAllAndFinish(); Assert.That(maxCount, Is.EqualTo(1), "There should never be more than one thread active at one time."); } }
public static void TestProgressing() { using (WorkerGroup workerGroup = new WorkerGroup()) { int percent = 0; workerGroup.Progress.Progressing += (object sender, ProgressEventArgs e) => { percent = e.Percent; }; IThreadWorker worker = workerGroup.CreateWorker(nameof(TestProgressing)); worker.Run(); workerGroup.WaitAllAndFinish(); Assert.That(percent, Is.EqualTo(100), "Progress at 100 percent should always be reported when the thread completes."); } }
public static void TestExplicitConstructor() { ProgressContext progress = new ProgressContext(); int percent = 0; progress.Progressing += (object sender, ProgressEventArgs e) => { percent = e.Percent; }; using (WorkerGroup workerGroup = new WorkerGroup(1, progress)) { IThreadWorker worker = workerGroup.CreateWorker(nameof(TestExplicitConstructor)); worker.Run(); workerGroup.WaitAllAndFinish(); } Assert.That(percent, Is.EqualTo(100), "Progress at 100 percent should always be reported when the thread completes."); }
private void itemAdd_Click(object sender, EventArgs args) { ToolStripDropDownItem item = sender as ToolStripDropDownItem; WorkerGroup group = item.Tag as WorkerGroup; foreach (Worker worker in SelectedItems) { if (worker.Groups.Contains(group)) { return; } worker.Groups.Add(group); } foreach (Worker worker in SelectedItems) { worker.MarkForSave = true; worker.Save(); } }
public ActionResult SaveGroup(int ID, string Name, string Participants) { var group = DB.WorkerGroups.FirstOrDefault(x => x.ID == ID); if (group == null) { group = new WorkerGroup() { OwnerID = CurrentUser.ShopOwnerID }; DB.WorkerGroups.InsertOnSubmit(group); } group.Name = Name; DB.SubmitChanges(); var pIds = Participants.Split <int>(";").ToList(); var forDel = group.WorkerGroupParticipants.ToList().Where(x => !pIds.Contains(x.WorkerID)).ToList(); var forAdd = pIds.Where(x => @group.WorkerGroupParticipants.All(z => z.WorkerID != x)).ToList(); if (forDel.Any()) { DB.WorkerGroupParticipants.DeleteAllOnSubmit(forDel); DB.SubmitChanges(); } if (forAdd.Any()) { DB.WorkerGroupParticipants.InsertAllOnSubmit(forAdd.Select(x => new WorkerGroupParticipant() { GroupID = group.ID, WorkerID = x })); DB.SubmitChanges(); } return(new ContentResult() { Content = "1" }); }
/// <summary> /// The citizen tries to build a building. /// </summary> /// <param name="building">The building that has to be build.</param> public void Build(GameObject building) { targetObject = building; stateMachine.SetState(CitizenState_Build.Instance); group = WorkerGroup.Builder; }
/// <summary> /// The citizen moves to a location, ignoring enemies. /// </summary> /// <param name="targetLocation">The target location.</param> public void MoveTo(Vector3 targetLocation) { navMeshAgent.SetDestination(targetLocation); stateMachine.SetState(CitizenState_Move.Instance); group = WorkerGroup.Other; }
/// <summary> /// The citizen tries to attack an enemy object (Unit/Building). /// </summary> /// <param name="target">The enemy object (Unit/Building).</param> public void Attack(GameObject target) { targetObject = target; stateMachine.SetState(CitizenState_Attack.Instance); group = WorkerGroup.Fighter; }
/// <summary> /// The citizen deliveres the currently holding resources to the target building. /// </summary> /// <param name="building">The building that stores the resources of the citizen.</param> public void DeliverResources(Building building) { targetObject = building.gameObject; stateMachine.SetState(CitizenState_DeliverResource.Instance); group = WorkerGroup.Other; }
/// <summary> /// The citizen starts collecting a resource. /// </summary> /// <param name="resource">The initial resource.</param> public void GatherResource(Resource resource) { targetResource = resource; stateMachine.SetState(CitizenState_GatherResource.Instance); group = SetWorkerGroup(resource); }
public Task <ProgressResult> Start( JobDTO job, CancellationToken cancellationToken, PauseToken pauseToken, ProgressToken progressToken) { if (IsCrawling) { throw new CrawlerAlreadyRunningException(); } return(Task.Run(() => { var workerRelevantJobData = WorkerRelevantJobData.FromJobDTO(job); // create worker groups (max 64 threads, could give them each a browser, can give them each a set of domains to work on) var group = new WorkerGroup( _browser, WorkersPerGroup); var uriQueue = new ConcurrentQueue <Uri>(job.Seeds); var htmlQueue = new ConcurrentQueue <string>(); var crawled = new ConcurrentDictionary <Uri, CrawlData>(); group.StartWorkers( pauseToken, workerRelevantJobData, uriQueue, crawled); IsCrawling = true; // main thread will spin here, checking for cancellation, or progress requests // quit if the queue is empty and no one seems to be adding to it? // don't want to just quit in case the queue is empty because there might be something about to be added // but it's just taking a long time while (!cancellationToken.IsCancellationRequested /* TODO add other stop conditions such as max crawl time */) { // cancellation and pauses are passed down to worker groups // progress is checked via the collections and then updated if (progressToken.ProgressIsRequested) { progressToken.State = GetState(uriQueue, crawled); } Thread.Sleep(10); } // tell the worker group to stop group.Cancel(); var start = DateTime.Now; // after cancellation is requested we fall into another loop, this has a time limit, so if not all worker groups are done while (!group.AllDone && (DateTime.Now - start) < AbortWorkersTimeout) { // notify of which threads are holding us up Thread.Sleep(10); } // attempt to properly join the threads if (group.AllDone) { group.DiposeWorkers(); } // abort them if not done else { group.AbortWorkers(); } IsCrawling = false; return GetResult(uriQueue, crawled); })); }
private void contextMenuStrip1_Opening(object sender, CancelEventArgs e) { if (SelectedItems.Count() == 0 || (!_showContextMenu && !_showSimpleContextMenu)) { e.Cancel = true; } else { //disableToolStripMenuItem.Visible = SelectedItem.idStatus == Worker.Status.Active; //enableToolStripMenuItem.Visible = SelectedItem.idStatus == Worker.Status.Inactive; if (_showSimpleContextMenu) { editToolStripMenuItem.Visible = false; duplicateUserToolStripMenuItem.Visible = false; disableToolStripMenuItem.Visible = false; enableToolStripMenuItem.Visible = false; deleteToolStripMenuItem.Visible = false; emailRotaToolStripMenuItem.Visible = false; toolStripSeparator2.Visible = false; toolStripSeparator3.Visible = false; } else { editToolStripMenuItem.Visible = _allowOpenWorker; } // ok first load up all the groups List <WorkerGroup> groups = WorkerGroupManager.Instance.WorkerGroups; List <WorkerGroup> groupsToAdd = new List <WorkerGroup>(groups); List <WorkerGroup> groupsToRemove = new List <WorkerGroup>(); // now remove all the groups to ADD except the <new group> one for (int i = addToGroupToolStripMenuItem.DropDownItems.Count - 1; i > 0; i--) { addToGroupToolStripMenuItem.DropDownItems.RemoveAt(i); } //now remove all the groups to REMOVE. removeFromGroupToolStripMenuItem.DropDownItems.Clear(); //========================================= // Add Section // // Here we are going to be adding groups // that none of the selected workers are // part of. //========================================= //first lets get all the groups that exist in all the selected items. List <WorkerGroup> selectedItemsGroups = new List <WorkerGroup>(); // create a list of every group, some may occur multiple times. we can jsut use this to see if its for everyone foreach (Worker worker in SelectedItems) { foreach (WorkerGroup group in worker.Groups) { selectedItemsGroups.Add(group); } } while (selectedItemsGroups.Count() > 0) { //get the last group in the list WorkerGroup group = selectedItemsGroups[selectedItemsGroups.Count - 1]; selectedItemsGroups.RemoveAt(selectedItemsGroups.Count - 1); int count = 1; //now find any dupes! for (int i = selectedItemsGroups.Count - 1; i >= 0; i--) { if (selectedItemsGroups[i] == group) { count++; selectedItemsGroups.RemoveAt(i); } } if (count == SelectedItems.Count()) { //everyone selected is in this group! groupsToAdd.Remove(group); } else { //only some people were in this group //leave it in } groupsToRemove.Add(group); } foreach (WorkerGroup group in groupsToAdd) { ToolStripItem item = new ToolStripMenuItem() { Text = group.GroupName, Tag = group }; item.Click += itemAdd_Click; addToGroupToolStripMenuItem.DropDownItems.Add(item); } foreach (WorkerGroup group in groupsToRemove) { ToolStripItem item = new ToolStripMenuItem() { Text = group.GroupName, Tag = group }; item.Click += itemRemove_Click; removeFromGroupToolStripMenuItem.DropDownItems.Add(item); } //only show the remove menu if there are groups to remove! removeFromGroupToolStripMenuItem.Visible = removeFromGroupToolStripMenuItem.DropDownItems.Count != 0; } }
public async Task Stop() { await Channel.CloseAsync(); await WorkerGroup.ShutdownGracefullyAsync(); }