public void ChainedDelegatesPostExecute() { SmartThreadPool smartThreadPool = new SmartThreadPool(); IWorkItemsGroup workItemsGroup = smartThreadPool.CreateWorkItemsGroup(int.MaxValue); PostExecuteWorkItemCallback postExecuteWorkItemCallback = new PostExecuteWorkItemCallback(DoPostExecute); postExecuteWorkItemCallback += new PostExecuteWorkItemCallback(DoPostExecute); workItemsGroup.QueueWorkItem( new WorkItemCallback(DoWork), null, postExecuteWorkItemCallback); workItemsGroup.WaitForIdle(); smartThreadPool.Shutdown(); }
public void WaitAllWithTimeoutFailure() { SmartThreadPool smartThreadPool = new SmartThreadPool(); IWorkItemsGroup workItemsGroup = smartThreadPool.CreateWorkItemsGroup(int.MaxValue); IWorkItemResult [] wirs = new IWorkItemResult[5]; for (int i = 0; i < wirs.Length; ++i) { wirs[i] = workItemsGroup.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null); } bool timeout = !SmartThreadPool.WaitAll(wirs, 10, true); bool success = timeout; smartThreadPool.Shutdown(); Assert.IsTrue(success); }
public void BlockingCall() { SmartThreadPool smartThreadPool = new SmartThreadPool(); IWorkItemsGroup workItemsGroup = smartThreadPool.CreateWorkItemsGroup(int.MaxValue); bool success = false; IWorkItemResult wir = workItemsGroup.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null); if (!wir.IsCompleted) { int result = (int)wir.GetResult(); success = (1 == result); } smartThreadPool.Shutdown(); Assert.IsTrue(success); }
public void TestWIGConcurrencyChange1WIG() { SmartThreadPool smartThreadPool = new SmartThreadPool(10 * 1000, 1, 0); Assert.IsTrue(0 == smartThreadPool.WaitingCallbacks); PauseSTP(smartThreadPool); Thread.Sleep(100); Assert.IsTrue(0 == smartThreadPool.WaitingCallbacks); IWorkItemsGroup wig = smartThreadPool.CreateWorkItemsGroup(1); wig.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null); Assert.IsTrue(1 == smartThreadPool.WaitingCallbacks); wig.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null); Assert.IsTrue(1 == smartThreadPool.WaitingCallbacks); wig.Concurrency = 2; Thread.Sleep(100); Assert.IsTrue(2 == smartThreadPool.WaitingCallbacks); ResumeSTP(smartThreadPool); Thread.Sleep(100); Assert.IsTrue(0 == smartThreadPool.WaitingCallbacks); PauseSTP(smartThreadPool); wig.Concurrency = 1; wig.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null); Assert.IsTrue(1 == smartThreadPool.WaitingCallbacks); wig.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null); Assert.IsTrue(1 == smartThreadPool.WaitingCallbacks); ResumeSTP(smartThreadPool); Thread.Sleep(100); Assert.IsTrue(0 == smartThreadPool.WaitingCallbacks); smartThreadPool.Shutdown(); }
public void WaitForIdle() { SmartThreadPool smartThreadPool = new SmartThreadPool(10 * 1000, 25, 0); IWorkItemsGroup workItemsGroup = smartThreadPool.CreateWorkItemsGroup(int.MaxValue); ManualResetEvent isRunning = new ManualResetEvent(false); for (int i = 0; i < 4; ++i) { workItemsGroup.QueueWorkItem(delegate { isRunning.WaitOne(); }); } bool success = !workItemsGroup.WaitForIdle(1000); isRunning.Set(); success = success && workItemsGroup.WaitForIdle(1000); smartThreadPool.Shutdown(); Assert.IsTrue(success); }
public void WaitAll() { SmartThreadPool smartThreadPool = new SmartThreadPool(); IWorkItemsGroup workItemsGroup = smartThreadPool.CreateWorkItemsGroup(int.MaxValue); bool success = true; IWorkItemResult [] wirs = new IWorkItemResult[5]; for (int i = 0; i < wirs.Length; ++i) { wirs[i] = workItemsGroup.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null); } SmartThreadPool.WaitAll(wirs); for (int i = 0; i < wirs.Length; ++i) { if (!wirs[i].IsCompleted) { success = false; break; } else { int result = (int)wirs[i].GetResult(); if (1 != result) { success = false; break; } } } smartThreadPool.Shutdown(); Assert.IsTrue(success); }
public void WaitAnyWithTimeoutSuccess() { SmartThreadPool smartThreadPool = new SmartThreadPool(); IWorkItemsGroup workItemsGroup = smartThreadPool.CreateWorkItemsGroup(int.MaxValue); bool success; IWorkItemResult [] wirs = new IWorkItemResult[5]; for (int i = 0; i < wirs.Length; ++i) { wirs[i] = workItemsGroup.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null); } int index = SmartThreadPool.WaitAny(wirs, 1500, true); success = (index != WaitHandle.WaitTimeout); smartThreadPool.Shutdown(); Assert.IsTrue(success); }
public void Timeout() { SmartThreadPool smartThreadPool = new SmartThreadPool(); IWorkItemsGroup workItemsGroup = smartThreadPool.CreateWorkItemsGroup(int.MaxValue); bool success = false; IWorkItemResult wir = workItemsGroup.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null); try { wir.GetResult(500, true); } catch (WorkItemTimeoutException) { success = true; } smartThreadPool.Shutdown(); Assert.IsTrue(success); }
/// <summary> /// Example of how to queue a work item and then cancel it while it is in the queue. /// </summary> private bool DoTestPostExecuteWithCancel(CallToPostExecute callToPostExecute, bool answer) { // Create a SmartThreadPool with only one thread. // It just to show how to use the work item canceling feature SmartThreadPool smartThreadPool = new SmartThreadPool(); IWorkItemsGroup workItemsGroup = smartThreadPool.CreateWorkItemsGroup(1); bool success = false; PostExecuteResult postExecuteResult = new PostExecuteResult(); // Queue a work item that will occupy the thread in the pool workItemsGroup.QueueWorkItem( new WorkItemCallback(this.DoSomeWork), null); // Queue another work item that will wait for the first to complete IWorkItemResult wir = workItemsGroup.QueueWorkItem( new WorkItemCallback(this.DoSomeWork), postExecuteResult, new PostExecuteWorkItemCallback(this.DoSomePostExecuteWork), callToPostExecute); // Wait a while for the thread pool to start executing the first work item Thread.Sleep(100); // Cancel the second work item while it still in the queue if (wir.Cancel()) { success = (postExecuteResult.wh.WaitOne(1000, true) == answer); } smartThreadPool.Shutdown(); return(success); }
public void DisposeCallerState() { SmartThreadPool smartThreadPool = new SmartThreadPool(); WIGStartInfo wigStartInfo = new WIGStartInfo(); wigStartInfo.DisposeOfStateObjects = true; IWorkItemsGroup workItemsGroup = smartThreadPool.CreateWorkItemsGroup(int.MaxValue, wigStartInfo); CallerState nonDisposableCallerState = new NonDisposableCallerState(); CallerState disposableCallerState = new DisposableCallerState(); IWorkItemResult wir1 = workItemsGroup.QueueWorkItem( new WorkItemCallback(this.DoSomeWork), nonDisposableCallerState); IWorkItemResult wir2 = workItemsGroup.QueueWorkItem( new WorkItemCallback(this.DoSomeWork), disposableCallerState); wir1.GetResult(); Assert.AreEqual(1, nonDisposableCallerState.Value); wir2.GetResult(); // Wait a little bit for the working thread to call dispose on the // work item's state. workItemsGroup.WaitForIdle(); Assert.AreEqual(2, disposableCallerState.Value); smartThreadPool.Shutdown(); }
public void DoWork(object [] states) { SmartThreadPool smartThreadPool = new SmartThreadPool(); WIGStartInfo wigStartInfo = new WIGStartInfo(); wigStartInfo.StartSuspended = true; IWorkItemsGroup wig = smartThreadPool.CreateWorkItemsGroup(1, wigStartInfo); foreach (object state in states) { wig.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), state); } // Start working on the work items in the work items group queue wig.Start(); // Wait for the completion of all work items wig.WaitForIdle(); smartThreadPool.Shutdown(); }
public void CancelWIGWorkItems() { // I don't use lock on the counter, since any number above 0 is a failure. // In the worst case counter will be equal to 1 which is still not 0. int counter = 0; SmartThreadPool stp = new SmartThreadPool(); IWorkItemsGroup wig = stp.CreateWorkItemsGroup(10); for (int i = 0; i < 10; i++) { wig.QueueWorkItem( state => { Thread.Sleep(500); ++counter; return(null); } ); } Thread.Sleep(100); wig.Cancel(true); Assert.AreEqual(counter, 0); stp.Shutdown(); }
public void Init() { _stp = new SmartThreadPool(); _wig = _stp.CreateWorkItemsGroup(10); }
/// <summary> /// Sends the email to recipients. /// </summary> /// <param name="site">The site.</param> /// <param name="documents">The updated documents.</param> public void SendEmailToRecipients(string site, int batchId) { try { List <Subscription> subscribers = SubscriptionService.GetCurrentSubscribers(site); if (null == subscribers || subscribers.Count == 0) { log.Info("No Subscribers - email will not be sent"); return; } string emailTemplateURL = null; string emailSubject = null; using (NotificationTablesDataContext dataContext = new NotificationTablesDataContext()) { SiteConfiguration siteConfiguration = dataContext.SiteConfigurations.FirstOrDefault(siteName => siteName.site == site); emailTemplateURL = siteConfiguration.emailTemplateURL; emailSubject = siteConfiguration.siteName + " " + (ConfigurationManager.AppSettings["Email.Subject"] as String); } string messageBody = GetEmailBody(emailTemplateURL + "?batchId=" + batchId); SmartThreadPool smartThreadPool = new SmartThreadPool(); IWorkItemsGroup workItemsGroup = smartThreadPool.CreateWorkItemsGroup(maxThreads); foreach (Subscription subscription in subscribers) { Email email = new Email { to = subscription.email, from = FromAddress, body = messageBody, subject = emailSubject }; PostExecuteWorkItemCallback afterEmailSend = delegate { SaveSentMailInformation(site, batchId, email); }; WorkItemInfo workItem = new WorkItemInfo(); workItem.PostExecuteWorkItemCallback = afterEmailSend; workItemsGroup.QueueWorkItem(workItem, SendMail, email); } workItemsGroup.WaitForIdle(); smartThreadPool.Shutdown(); using (NotificationTablesDataContext dataContext = new NotificationTablesDataContext()) { Batch batch = dataContext.Batches.FirstOrDefault(b => b.site == site && b.batchId == batchId); batch.finishDate = DateTime.Now; batch.status = "Successful"; dataContext.SubmitChanges(); } } catch (Exception e) { log.Error("Unable to Send Email", e); using (NotificationTablesDataContext dataContext = new NotificationTablesDataContext()) { Batch batch = dataContext.Batches.FirstOrDefault(b => b.site == site && b.batchId == batchId); batch.status = "Unsuccessful"; dataContext.SubmitChanges(); } throw e; } }
public static IWorkItemsGroup getWorkItemsGroup() { return(threadPool.CreateWorkItemsGroup(threadCntPerSite)); }
private void DoUpdateProjectsInternal() { var projectsByServer = ConfigurationService.GetProjects(); var allWorkItemsGroup = new List <IWorkItemsGroup>(); var allFutureBuildDetails = new Dictionary <Project, IWorkItemResult>(); foreach (var pair in projectsByServer) { var server = pair.Key; var projects = pair.Value; var workItemsGroup = threadPool.CreateWorkItemsGroup(THREAD_COUNT_BY_DOMAIN); allWorkItemsGroup.Add(workItemsGroup); foreach (var project in projects) { WorkItemCallback work = delegate(object state) { AllBuildDetails newBuildDetail = null; try { var project_ = (Project)state; newBuildDetail = JenkinsService.UpdateProject(project_); } catch (Exception ex) { LoggingHelper.LogError(logger, ex); } return(newBuildDetail); }; var futureRes = workItemsGroup.QueueWorkItem(work, project); allFutureBuildDetails[project] = futureRes; } } foreach (var workItemsGroup in allWorkItemsGroup) { workItemsGroup.WaitForIdle(); } foreach (var projects in projectsByServer.Values) { foreach (var project in projects) { IWorkItemResult newStatus; allFutureBuildDetails.TryGetValue(project, out newStatus); var previousAllBuildDetails = project.AllBuildDetails; if (newStatus != null) { project.AllBuildDetails = (AllBuildDetails)newStatus.Result; project.Activity.HasNewBuild = false; if (previousAllBuildDetails != null && project.AllBuildDetails != null) { project.PreviousStatus = previousAllBuildDetails.Status; if (previousAllBuildDetails.LastBuild != null && project.AllBuildDetails.LastBuild != null) { // Has existing LastBuilds if (previousAllBuildDetails.LastBuild.Number != project.AllBuildDetails.LastBuild.Number) { project.Activity.HasNewBuild = true; } } else if (previousAllBuildDetails.LastBuild == null && project.AllBuildDetails.LastBuild != null) { // 1st new LastBuild is found project.Activity.HasNewBuild = true; } } } } } if (ProjectsUpdated != null) { ProjectsUpdated(); } }
/// <summary> /// 实例化CastleService /// </summary> /// <param name="config"></param> public CastleService(CastleServiceConfiguration config) { this.config = config; if (string.Compare(config.Host, "any", true) == 0) { config.Host = IPAddress.Loopback.ToString(); epServer = new ScsTcpEndPoint(config.Port); } else { epServer = new ScsTcpEndPoint(config.Host, config.Port); } this.server = ScsServerFactory.CreateServer(epServer); this.server.ClientConnected += server_ClientConnected; this.server.ClientDisconnected += server_ClientDisconnected; this.server.WireProtocolFactory = new CustomWireProtocolFactory(config.Compress, config.Encrypt); //服务端注入内存处理 this.container = new SimpleServiceContainer(CastleFactoryType.Local); this.container.OnError += error => { if (OnError != null) { OnError(error); } }; this.container.OnLog += (log, type) => { if (OnLog != null) { OnLog(log, type); } }; //实例化SmartThreadPool var stp = new STPStartInfo { IdleTimeout = config.Timeout * 1000, MaxWorkerThreads = Math.Max(config.MaxCalls, 10), MinWorkerThreads = 5, ThreadPriority = ThreadPriority.Normal, WorkItemPriority = WorkItemPriority.Normal }; //创建线程池 smart = new SmartThreadPool(stp); smart.Start(); //创建并发任务组 var group = smart.CreateWorkItemsGroup(2); group.Start(); //实例化调用者 var status = new ServerStatusService(server, config, container); this.caller = new ServiceCaller(group, status); //判断是否启用httpServer if (config.HttpEnabled) { //设置默认的解析器 IHttpApiResolver resolver = new DefaultApiResolver(); //判断是否配置了HttpType if (config.HttpType != null && typeof(IHttpApiResolver).IsAssignableFrom(config.HttpType)) { resolver = Activator.CreateInstance(config.HttpType) as IHttpApiResolver; } var httpCaller = new HttpServiceCaller(group, config, container); //刷新服务委托 status.OnRefresh += () => httpCaller.InitCaller(resolver); //初始化调用器 httpCaller.InitCaller(resolver); var handler = new HttpServiceHandler(httpCaller); var factory = new HttpRequestHandlerFactory(handler); this.httpServer = new HTTPServer(factory, config.HttpPort); } //绑定事件 MessageCenter.Instance.OnError += Instance_OnError; }
public void Init() { _stp = new SmartThreadPool(); _wig = _stp.CreateWorkItemsGroup(SmartThreadPool.DefaultMaxWorkerThreads); }
public override void PostApply(BackgroundWorker worker, DoWorkEventArgs e) { try { Dictionary <string, string> error = new Dictionary <string, string>(); List <string> successful = new List <string>(); int num = 0; progressStepCount = 5; List <DriverPackage> driverPackages = (List <DriverPackage>)UserData["DriverPackageItems"]; totalDriverPackages = driverPackages.Count; foreach (DriverPackage driverPackage in driverPackages) { progressStart = num * 100 / totalDriverPackages; worker.ReportProgress(progressStart, string.Format("Importing Driver Package: {0}", driverPackage.Name)); if (driverPackage.Create()) { string objectPath = null; refreshPackage = false; refreshDP = false; progressTotal = 0; progressCount = 0; try { SmartThreadPool smartThreadPool = new SmartThreadPool(); // lock driver packge object objectPath = driverPackage.Package["__RELPath"].StringValue; Utility.RequestLock(ConnectionManager, objectPath); int totalInfs = driverPackage.Infs.Length; int num2 = 1; // parse ini files and create a dictinonary with the results log.Debug("ProcessingINFs ---- "); foreach (string inf in driverPackage.Infs) { // I still hate calculating progress bars progresPercent = (num2 / totalInfs * 100) / progressStepCount / totalDriverPackages; worker.ReportProgress( progressStart + (progresPercent), string.Format("Importing Driver Package: {0}\n - processing inf '{1}'", driverPackage.Name, Path.GetFileName(inf)) ); log.Debug("ProcessingINF: " + inf); Driver driver = new Driver(inf); if (driver.HasWarning) { driverPackage.ImportWarning[inf] = driver.Warning.Message; } else if (driver.HasException) { driverPackage.ImportError[inf] = driver.Exception.Message; } else { // need to check versions to for duplicate drivers with differnt version number driverPackage.Drivers[Path.GetFileName(inf) + driver.Version] = driver; } num2++; } // I still hate calculating progress bars progressStepPercent = progresPercent; progresPercent = 100 / progressStepCount / totalDriverPackages * 2; log.Debug("ProcessingDrivers ---- "); // check if driver is in driver package and same version foreach (IResultObject driverObject in driverPackage.GetDriversInPackge()) { // thread this so it is faster smartThreadPool.QueueWorkItem(new Amib.Threading.Func <DriverPackage, IResultObject, BackgroundWorker, bool>(ProcessDrivers), driverPackage, driverObject, worker); } // wait for thread pool to finish smartThreadPool.WaitForIdle(); // I still hate calculating progress bars progressStepPercent = progresPercent; log.Debug("ImportingDrivers ---- "); // tried to create threading for importing drivers but CreateFromINF go sad if it gets to many request, so will leave this code here for maybe one day I fix it IWorkItemsGroup workItemsGroup = smartThreadPool.CreateWorkItemsGroup(1); foreach (KeyValuePair <string, Driver> driver in driverPackage.Drivers) { if (driver.Value.Import) { ++progressTotal; workItemsGroup.QueueWorkItem(new Amib.Threading.Func <DriverPackage, Driver, BackgroundWorker, bool>(ImportDrivers), driverPackage, driver.Value, worker); } } // wait for thread pool to finish workItemsGroup.WaitForIdle(); smartThreadPool.Shutdown(); if (refreshPackage) { log.Debug("RefreshPackage ---- "); // I still hate calculating progress bars progresPercent = 100 / progressStepCount / totalDriverPackages * 4; worker.ReportProgress(progressStart + (progresPercent), string.Format("Importing Driver Package: {0}\n - adding drivers to package", driverPackage.Name)); // add all drivers in the drivers list to the driver package driverPackage.AddDriversToDriverPack(); } if (refreshDP) { log.Debug("RefreshDP ---- "); // I still hate calculating progress bars progresPercent = 100 / progressStepCount / totalDriverPackages * 5; worker.ReportProgress(progressStart + (progresPercent), string.Format("Importing Driver Package: {0}\n - updating distribution point", driverPackage.Name)); driverPackage.Package.ExecuteMethod("RefreshPkgSource", null); } log.Debug("CreateHashFile ---- "); driverPackage.CreateHashFile(); log.Debug("UpdatePackageVersion ---- "); driverPackage.UpdatePackageVersion(); } finally { // unlock driver package object Utility.ReleaseLock(ConnectionManager, objectPath); } } ++num; } PrepareCompletion(); base.PostApply(worker, e); } catch (Exception ex) { AddRefreshResultObject(null, PropertyDataUpdateAction.RefreshAll); PrepareError(ex.Message); } }