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


C# TaskCompletionSource.Start方法代码示例

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


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

示例1: RunRefactoringTests_NegativeTests

        public static void RunRefactoringTests_NegativeTests()
        {
            TaskCompletionSource<int> tr = new TaskCompletionSource<int>();
            int temp = 0;
            Task<int> f = Task.Factory.StartNew<int>((object i) => { return (int)i; }, 1, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Current);
            Task t;
            temp = f.Result;
            if (temp != 1)
            {
                Assert.True(false, string.Format("RunRefactoringTests - Task.Factory.StartNew<int>(Func<object, int>, object, CT, options, TaskScheduler).    > FAILED.  Delegate failed to execute."));
            }

            f = new TaskCompletionSource<int>().Task;
            try
            {
                f.Start();
                Assert.True(false, string.Format("RunRefactoringTests - TaskCompletionSource<int>.Task (should throw exception):    > FAILED.  No exception thrown."));
            }
            catch (Exception)
            {
                //Assert.True(false, string.Format("    > caught exception: {0}", e.Message));
            }

            t = new Task(delegate { temp = 100; });
            t.Start();
            try
            {
                t.Start();
                Assert.True(false, string.Format("RunRefactoringTests - Restarting Task:    > FAILED.  No exception thrown, when there should be."));
            }
            catch (Exception)
            {
                //Assert.True(false, string.Format("    > caught exception: {0}", e.Message));
            }

            // If we don't do this, the asynchronous setting of temp=100 in the delegate could
            // screw up some tests below.
            t.Wait();

            try
            {
                t = new Task(delegate { temp = 100; }, (TaskCreationOptions)10000);
                Assert.True(false, string.Format("RunRefactoringTests - Illegal Options CTor Task:    > FAILED.  No exception thrown, when there should be."));
            }
            catch (Exception) { }

            try
            {
                t = new Task(null);
                Assert.True(false, string.Format("RunRefactoringTests - Task ctor w/ null action:    > FAILED.  No exception thrown."));
            }
            catch (Exception) { }

            try
            {
                t = Task.Factory.StartNew(null);
                Assert.True(false, string.Format("RunRefactoringTests - Task.Factory.StartNew() w/ Null Action:    > FAILED.  No exception thrown."));
            }
            catch (Exception) { }

            t = new Task(delegate { });
            Task t2 = t.ContinueWith(delegate { });
            try
            {
                t2.Start();
                Assert.True(false, string.Format("RunRefactoringTests - Task.Start() on Continuation Task:    > FAILED.  No exception thrown."));
            }
            catch (Exception) { }

            t = new Task(delegate { });
            try
            {
                t.Start(null);
                Assert.True(false, string.Format("RunRefactoringTests - Task.Start() with null taskScheduler:    > FAILED.  No exception thrown."));
            }
            catch (Exception) { }

            t = Task.Factory.StartNew(delegate { });
            try
            {
                t = Task.Factory.StartNew(delegate { }, CancellationToken.None, TaskCreationOptions.None, (TaskScheduler)null);
                Assert.True(false, string.Format("RunRefactoringTests - Task.Factory.StartNew() with null taskScheduler:    > FAILED.  No exception thrown."));
            }
            catch (Exception) { }

            tr = new TaskCompletionSource<int>();
            tr.SetException(new Exception("some exception"));
            try
            {
                tr.SetResult(5);
                Assert.True(false, string.Format("RunRefactoringTests - TaskCompletionSource set Result after setting Exception:     > FAILED.  No exception thrown."));
            }
            catch (Exception)
            { }
            finally
            {
                //prevent finalize from crashing on exception
                Exception e2 = tr.Task.Exception;
            }

//.........这里部分代码省略.........
开发者ID:er0dr1guez,项目名称:corefx,代码行数:101,代码来源:TaskRtTests_Core.cs


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