jjzjj

ContinueWith

全部标签

c# - 为什么 Task.ContinueWith 在此单元测试中执行失败?

我遇到了一个单元测试失败的问题,因为TPL任务从未执行过它的ContinueWith(x,TaskScheduler.FromCurrentSynchronizationContext())。问题原来是因为在任务启动之前不小心创建了一个WinformsUI控件。这是一个重现它的例子。您会看到,如果您按原样运行测试,它就会通过。如果您在未注释Form行的情况下运行测试,它将失败。[TestClass]publicclassUnitTest1{[TestMethod]publicvoidTestMethod1(){//CreatenewsynccontextforunittestSynch

c# - 等价于 ContinueWith(delegate, CancellationToken) with await continuation

我有这种情况:privateTaskLongRunningTask=/*Something*/;privatevoidDoSomethingMore(TaskpreviousTask){}publicTaskIndependentlyCancelableSuccessorTask(CancellationTokencancellationToken){returnLongRunningTask.ContinueWith(DoSomethingMore,cancellationToken);}特别是,我感兴趣的行为在MSDN'spageaboutContinuationTasks中有详细

c# - 在 ContinueWith 中观察任务异常

有多种方法可以观察任务中抛出的异常。其中之一在ContinueWith和OnlyOnFaulted中:vartask=Task.Factory.StartNew(()=>{//Throwsanexception//(possiblyfromwithinanothertaskspawnedfromwithinthistask)});varfailureTask=task.ContinueWith((t)=>{//Flattenandloop(sincetherecouldhavebeenmultipletasks)foreach(varexint.Exception.Flatten().

c# - 如何安排有条件的 ContinueWith

我在一堆LINQ查询上有一些GUI。查询需要一些时间来执行,所以我希望GUI能够响应并显示繁忙指示器和进度条。许多查询是为了检查数据中存在的某些条件。如果查询返回空结果,应用程序应继续下一个查询。如果它返回结果,则返回集的严重性将是“警告”或“错误”。如果是警告,则继续执行。如果出错,则停止。许多代码与线程池和GUI玩“乒乓球”。准代码:TaskFactory.StartNew(()=>{Runinbackground}.ContinueInGui(()=>{Updatesomething}).ContinueInBackground(()=>{Domorework;}).Contin

c# - Task.ContinueWith 是否捕获调用线程上下文以继续?

下面的Test_Click是在UI线程上运行的代码的简化版本(带有WindowsFormsSynchronizationContext):voidTest_Click(objectsender,EventArgse){vartask=DoNavigationAsync();task.ContinueWith((t)=>{MessageBox.Show("Navigationdone!");},TaskScheduler.FromCurrentSynchronizationContext());}我是否应该显式指定TaskScheduler.FromCurrentSynchronizat

c# - 什么时候应该以 TaskScheduler.Current 作为参数调用 Task.ContinueWith?

我们正在使用thiscodesnippet从StackOverflow生成一个任务,该任务在任务集合中的第一个任务成功完成后立即完成。由于其执行的非线性特性,async/await并不是真正可行的,因此此代码改用ContinueWith()。不过,它没有指定TaskScheduler,它是numberofsources已经提到可能是危险的,因为它使用TaskScheduler.Current而大多数开发人员通常期望来自延续的TaskScheduler.Default行为。普遍的看法似乎是您应该始终将显式的TaskScheduler传递给ContinueWith。但是,我还没有看到关于何

c# - 使用 Task.Factory.StartNew().ContinueWith() 的单元测试代码

所以我有一些代码Task.Factory.StartNew(()=>this.listener.Start()).ContinueWith((task)=>{if(task.IsCompleted){this.status=WorkerStatus.Started;this.RaiseStatusChanged();this.LogInformationMessage("WorkerStarted.");}});当我测试时,我正在模拟所有依赖对象(namleythis.listener.Start())。问题是测试在调用ContinueWith之前完成执行。当我调试时,由于我单步执行代

c# - 在延续链中传播异常的正确方法是什么?

在延续链中传播异常的正确方法是什么?t.ContinueWith(t2=>{if(t2.Exception!=null)throwt2.Exception;/*Otherasynccode.*/}).ContinueWith(/*...*/);t.ContinueWith(t2=>{if(t2.IsFaulted)throwt2.Exception;/*Otherasynccode.*/}).ContinueWith(/*...*/);t.ContinueWith(t2=>{if(t2.Exception!=null)returnt2;/*Otherasynccode.*/}).Con

c# - 如何根据最佳实践在 C# 4 中创建异步方法?

考虑以下代码片段:publicstaticTaskFetchAsync(){stringurl="http://www.example.com",message="HelloWorld!";varrequest=(HttpWebRequest)WebRequest.Create(url);request.Method=WebRequestMethods.Http.Post;returnTask.Factory.FromAsync(request.BeginGetRequestStream,request.EndGetRequestStream,null).ContinueWith(t=

c# - 顺序等待 VS 连续等待

我想知道编写由两个(或更多)异步和依赖(第一个必须完成才能执行第二个)操作组成的异步代码的最佳/正确方法是什么。异步/等待示例:awaitRunFirstOperationAsync();awaitRunSecondOperationAsync();继续的例子:awaitRunFirstOperationAsync().ContinueWith(t=>RunSecondOperationAsync()); 最佳答案 如果可能,您会希望使用await。ContinueWith有很多问题。我在我关于whyContinueWithisda