当前位置: 首页>>代码示例>>C#>>正文


C# Task.Any方法代码示例

本文整理汇总了C#中System.Threading.Tasks.Task.Any方法的典型用法代码示例。如果您正苦于以下问题:C# Task.Any方法的具体用法?C# Task.Any怎么用?C# Task.Any使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Threading.Tasks.Task的用法示例。


在下文中一共展示了Task.Any方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: TestSimpleParallelHttpPostRequests

        public void TestSimpleParallelHttpPostRequests()
        {
            var entity = new PerformanceEntity();
            entity.Id = "1234567890123456789012345678901234";
            entity.Key = "1234567";
            entity.Revision = "1234567";
            entity.Name = "Mohamad Abu Bakar";
            entity.IcNumber = "1234567-12-3444";
            entity.Department = "IT Department";
            entity.Height = 1234;
            entity.DateOfBirth = new DateTime(2015, 1, 27, 3, 33, 3);
            entity.Salary = 3333;

            var jsonEntity = JSON.ToJSON(entity);

            var threadCount = 10;
            var tasks = new Task[threadCount];

            Stopwatch stopwatch = Stopwatch.StartNew();

            for (int i = 0; i < threadCount; i++)
            {
                tasks[i] = Task.Factory.StartNew(() =>
                {
                    int messages_sent_by_one_task = 0;

                    while(messages_sent_by_one_task < 100)
                    {
                        SimpleHttpPostCreateDocument(
                            "http://localhost:8529/_db/" + Database.TestDatabaseGeneral + "/_api/document?collection=" + Database.TestDocumentCollectionName,
                            jsonEntity
                        );

                        messages_sent_by_one_task++;
                    }
                });
            }

            while (tasks.Any(t => !t.IsCompleted)) { }

            Console.WriteLine("Elapsed time [s]: {0}", stopwatch.Elapsed.TotalMilliseconds / 1000);
        }
开发者ID:kangkot,项目名称:ArangoDB-NET,代码行数:42,代码来源:Performance.cs

示例2: ScanImageSection

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ImgSec"></param>
        /// <param name="ColorForgive"></param>
        /// <param name="ColorDetail"></param>
        /// <param name="MinMargin"></param>
        /// <returns></returns>
        internal static Child[] ScanImageSection(ImageSection ImgSec, int ColorForgive, int ColorDetail, int MinMargin, Child Parent = null)
        {
#if GUIOUTPUT
            mScannedPx = 0;
            int MaxSize = ImgSec.Width * ImgSec.Height;
            frmImgSecDisplay DisplayOut = null;
            //DisplayOut.Show();
            //Master.sInvokable.Invoke(Master.sAddable, null => {return new frmImgSecDisplay();}, DisplayOut);
            MakeForm NewDisp = delegate() { return new frmImgSecDisplay(); };
            AssignForm AssDisp = delegate(System.Windows.Forms.Form ToAssign) { DisplayOut = (frmImgSecDisplay)ToAssign; };
            Master.sInvokable.Invoke(Master.sAddable, new object[] { NewDisp, AssDisp });
#endif
            //Break the image into ImageSections
            List<ImageSection> Sections = new List<ImageSection>();
            ImageSectionFactory Factory;
            for (int y = 0; y < ImgSec.Height; y++)
            {
                for (int x = 0; x < ImgSec.Width; x++)
                {
                    if (ImgSec.GetPixel(x, y).A == byte.MaxValue)
                    {
                        Factory = new ImageSectionFactory(new Point(x, y), ColorForgive, ColorDetail
#if GUIOUTPUT
                            ,DisplayOut
#endif
                            );
                        Sections.Add(Factory.Recognize(ref ImgSec, x, y));
#if GUIOUTPUT
                        UpdateDisplay((mScannedPx += Factory.SelectedPixels().Count()), MaxSize, 0);
#endif
                    }
                }
            }

            Sections.RemoveAll(imsec => imsec == null);
            //Figure out if it just selected the whole thing. If so, cancel the scan
            if (Sections.Count == 1 && Sections[0].Size == ImgSec.Size)
                return new Child[0];

            //Figure out which ones of those are too small and should be discounted
            for (int remove = 0; remove < Sections.Count; remove++)
            {
                if (Sections[remove].PixelsUsed().Length <= MinMargin) Sections.RemoveAt(remove);
            }
            Sections.TrimExcess();

            // Once that's done, turn the remaining Image Sections into Children
            Child[] Children = new Child[Sections.Count];
#if MULTITHREAD
            Task<Child>[] Tasks = new Task<Child>[Sections.Count];
#endif
            for (int i = 0; i < Sections.Count; i++)
            {
#if MULTITHREAD
                Tasks[i] = new Task<Child>(s => Child.FromSection((Child)((object[])s)[0], (ImageSection)((object[])s)[1]), new object[]{Parent, Sections[i]});
                Tasks[i].Start();
#else
                Children[i] = Child.FromSection(Parent, Sections[i]);
#endif

#if GUIOUTPUT
                UpdateDisplay(MaxSize, MaxSize, 1);
#endif
            }
#if MULTITHREAD
            try
            {
                while (Tasks.Any(t => !t.IsCompleted)) Tasks.First(t => !t.IsCompleted).Wait();
            }
            catch (NullReferenceException) { }
            for (int i = 0; i < Tasks.Length; i++)
            {
                Children[i] = Tasks[i].Result;
            }
#endif

            return Children;
        }
开发者ID:scnerd,项目名称:Picasso,代码行数:86,代码来源:Child.cs

示例3: EvaluateFormula

        private static void EvaluateFormula(FormulaTree formulaTree, Range[] ranges, Size areaSize, int threadsCount, float[] evaluatedValuesBuffer)
        {
            Stopwatch evaluationStopwatch = new Stopwatch();
            evaluationStopwatch.Start();

            if (evaluatedValuesBuffer.Length != areaSize.Square)
                throw new ArgumentException("Result buffer size isn't equal to ranges area size.", "evaluatedValuesBuffer");

            int xCount = areaSize.Width;
            int yCount = areaSize.Height;

            if (threadsCount < 1)
                threadsCount = 1;

            ranges = ranges.Select((r, i) => new Range(r.Start, r.End, i % 2 == 0 ? areaSize.Width : areaSize.Height)).ToArray();
            const int progressSteps = 100;
            using (ProgressReporter.CreateScope(progressSteps))
            {
                int steps = 0;
                double lastProgress = 0;
                double progress = 0;
                ProgressObserver progressObserver = new ProgressObserver(p => progress = p.Progress);

                Task[] tasks = new Task[threadsCount];
                int yStepCount = yCount/threadsCount;
                for (int i = 0; i < threadsCount; i++)
                {
                    int li = i;
                    tasks[i] = Task.Run(() =>
                    {
                        FormulaTree ft = li == 0 ? formulaTree : FormulaTreeSerializer.Deserialize(FormulaTreeSerializer.Serialize(formulaTree));
                        int yStart = li * yStepCount;
                        ProgressReporter.Subscribe(progressObserver);
                        ft.EvaluateRangesIn2DProjection(ranges, xCount, yStart, yStepCount,
                            evaluatedValuesBuffer);
                    });
                }

                while (tasks.Any(t => t.Status != TaskStatus.RanToCompletion && t.Status != TaskStatus.Faulted && t.Status != TaskStatus.Canceled))
                {
                    Task.WaitAny(tasks, 100);
                    double progressCopy = progress;
                    int inc = (int)((progressCopy - lastProgress) * progressSteps);
                    if (inc > 0)
                    {
                        for (int i = 0; i < inc && steps < progressSteps; i++)
                        {
                            ProgressReporter.Increase();
                            steps++;
                        }
                        lastProgress = progress;
                    }
                }
                Task.WaitAll();
            }

            //double x = 1;
            //double y = 2;
            //double z = 3;
            //double w = 4;
            //double x1 = -1;
            //double y1 = -2;
            //double z1 = -3;
            //double w1 = -4;
            //Stopwatch stopwatch2 = new Stopwatch();
            //stopwatch2.Start();
            //double[] arr = new double[Ranges[0].Count*Ranges[1].Count];
            //for (int i = 0; i < arr.Length; i++)
            //{
            //    arr[i] = Math.Sqrt((Math.Sin(x) * Math.Sin(y) + Math.Sin(z) * Math.Sin(w)) * (Math.Sin(x1) * Math.Sin(y1) + Math.Sin(z1) * Math.Sin(w1)));
            //    x += 0.1;
            //    y += 0.1;
            //    z += 0.1;
            //    w += 0.1;
            //    x1 += 0.3;
            //    y1 += 0.3;
            //    z1 += 0.3;
            //    w1 += 0.3;
            //}
            //stopwatch2.Stop();

            evaluationStopwatch.Stop();
        }
开发者ID:es-repo,项目名称:wlpgnr,代码行数:83,代码来源:FormulaRender.cs

示例4: GetOutcomeAsync

 /// <summary>
 /// Gets the outcome for a full round of responses from all the clusters.
 /// </summary>
 /// <param name="responsePromises">Promises fot the responses for a particular grain from all of the clusters in the multi-cluster network</param>
 /// <param name="grainId">The ID of the grain that we want to know its owner status</param>
 /// <param name="logger">The logger in case there is useful information to log.</param>
 /// <returns>The outcome of aggregating all of the responses. The task will complete as soon as it has enough responses to make a determination, even if not all of the clusters responded yet.</returns>
 public static Task<GlobalSingleInstanceResponseOutcome> GetOutcomeAsync(Task<RemoteClusterActivationResponse>[] responsePromises, GrainId grainId, Logger logger)
 {
     if (responsePromises.Any(t => t == null)) throw new ArgumentException("All response promises should have been initiated", nameof(responsePromises));
     var details = new GlobalSingleInstanceResponseTracker(responsePromises, grainId, logger);
     return details.Task;
 }
开发者ID:osjimenez,项目名称:orleans,代码行数:13,代码来源:GlobalSingleInstanceResponseTracker.cs


注:本文中的System.Threading.Tasks.Task.Any方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。