Tag: 任务

在Task.WhenAll中完成任务时是否有回调

假设我有以下内容: IEnumerable<Task> tasks = //… TimeSpan[] results = await Task.WhenAll(tasks); // Handle results 当我能够处理结果时,所有任务必须完成。 有没有办法按需处理每个结果? 就像注册任务完成时将执行的委托/回调一样: IEnumerable<Task> tasks = //… await Task.WhenAll(tasks, result => { // A task has finished. This will get executed. // result is of type TimeSpan });

如何创建前台任务?

我似乎无法创建前台任务。 我的主线程是支持调用另一个线程然后退出。 另一个线程假设永远运行 void MainThreadMain() { task_main = Task.Factory.StartNew(() => OtherThread()) ; return; } void OtherThread() { while(true) { TellChuckNorrisJoke(); } } 即使主线程已经死了,我怎样才能确保task_main继续运行? 我假设他做了: task_main.IsBackgorund = false; 但没有这样的选择:\我可以让我的主线程等待来自我的其他线程的信号,它传递给Foreground模式。 但那简直太傻了。

异步方法抛出exception

我目前在使用抛出exception的方法时遇到了一些问题,但我不确定原因。 该exception使我的应用程序崩溃。 System.NullReferenceException: Object reference not set to an instance of an object. at Myapp.AutoProcess.d__36.MoveNext() — End of stack trace from previous location where exception was thrown — at System.Runtime.CompilerServices.AsyncMethodBuilderCore.b__1(Object state) at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem() at […]

wp8的任务队列?

在wp8中对复杂任务进行排队的正确方法是什么? 任务包括以下内容: 通过更新模型变量显示ProgressIndicator 获取或存储数据到wcf服务( UploadStringAsync ) 使用UploadStringCompleted的结果更新潜在的数据绑定模型。 通过更新模型变量隐藏ProgressIndicator 目前我一直在使用一个拥有命令对象队列的类,运行一个在添加项目时启动的线程(如果它尚未运行)。 但是,我有等待代码停止运行的任务或子任务的问题。 以前我曾经使用异步等待,但是行为的几个级别变得越来越难以预测。 我想要的是能够创建和排队命令对象的主线程。 命令对象应该一次运行一个,而不是在前一个完全完成之前启动一个新对象。 如果需要,命令对象应该能够使用调度程序访问主线程。

.NET 4.0中.NET 4.5的Task.FromResult()等效

我无法找到有关将我的代码从.NET 4.5重定向到4.0的信息。 我必须在Windows XP上安装此应用程序。 我在.NET 4.5中的代码 public async Task ImportFile(string fileToImport) { … return await Task.FromResult(Sheet1) } 在.NET 4.0方法中,FromResult不存在。 有人知道它应该在.NET 4.0中看起来怎么样?

串行任务执行器; 这个线程安全吗?

我使用ThreadPool作为执行手段,创建了一个允许异步顺序执行任务的类。 我的想法是,我将在后台运行串行任务的多个实例,但我不希望每个实例都有一个单独的专用线程。 我想检查的是这个类是否真的是线程安全的。 这是相当简短的,所以我想我会由专家在这里运行它,以防我遗漏了一些明显的东西。 我省略了一些针对不同Action类型的方便重载。 /// /// This class wraps ThreadPool.QueueUserWorkItem, but providing guaranteed ordering of queued tasks for this instance. /// Only one task in the queue will execute at a time, with the order of execution matching the order of addition. /// This is designed as a lighter-weight alternative to using a dedicated […]

执行进程的C#类中的异步方法

我对这篇文章有一个后续问题。 在我的版本中,我有以下我想要异步。 这是我有的: public virtual Task ExecuteAsync() { var tcs = new TaskCompletionSource(); string exe = Spec.GetExecutablePath(); string args = string.Format(“–input1={0} –input2={1}”, Input1, Input2); try { var process = new Process { EnableRaisingEvents = true, StartInfo = { UseShellExecute = false, FileName = exe, Arguments = args, RedirectStandardOutput = true, RedirectStandardError = true, WorkingDir = […]

为什么Type.IsGenericType为Task返回TRUE而没有通过方法reflection获得的返回类型但是typeof(Task).IsGenericTyp返回FALSE

有人可以解释一下吗? 每个文档IsGenericType 指示当前Type是否表示generics类型或方法的定义中的类型参数。 所以这个(LINQPad)代码: bool bStraight = typeof(Task).IsGenericType; bStraight.Dump(“typeof(Task).IsGenericType”); 按预期工作并产生输出: 的typeof(任务).IsGenericType 假 但是当我通过reflection从方法中检索它时: public class MyClass { public async Task Method() { await Task.Run(() => { Thread.Sleep(3000); }); } } public async Task TEST() { MyClass theObject = new MyClass(); Task task = (Task)typeof(MyClass).GetTypeInfo() .GetDeclaredMethod(“Method”) .Invoke(theObject, null); bool b = task.GetType().IsGenericType; bool b2 = task.GetType().GetGenericTypeDefinition() […]

具有多个任务和UI同步的WinForms TPL模式 – 这是正确的吗?

我是TPL(任务并行库)的新手,我想知道以下是否是最有效的方法来启动1个或多个任务,整理结果并在数据网格中显示它们。 Search1和Search2与两个单独的数据库通信,但返回相同的结果。 我禁用按钮并打开微调器。 我正在使用一个ContinueWhenAll方法调用来关闭任务。 我已将调度程序添加到ContinueWhenAll调用以更新表单按钮,datagrid,并关闭微调器。 问:我这样做是对的吗? 有没有更好的办法 ? 问:我如何为此添加取消/exception检查? 问:如果我需要添加进度报告 – 我该怎么做? 我选择这种方法的原因是,后台工作者是这样我可以并行地按顺序启动每个数据库任务。 除此之外,我认为使用TPL可能会很有趣..但是,因为我找不到任何具体的例子,我在下面做了什么(多个任务)我觉得把它放在这里得到它可能会很好答案,希望成为其他人的榜样。 谢谢! 码: // Disable buttons and start the spinner btnSearch.Enabled = btnClear.Enabled = false; searchSpinner.Active = searchSpinner.Visible = true; // Setup scheduler TaskScheduler scheduler = TaskScheduler.FromCurrentSynchronizationContext(); // Start the tasks Task.Factory.ContinueWhenAll( // Define the search tasks that return List new [] […]

不等待时TaskCanceledException

每当我同步返回另一个任务而不是等待它时,我似乎得到一个TaskCanceledException ,遵循最后等待时的指导原则。 TaskCanceledException代码 public static class Download { public static Task FromYouTubeAsync(string videoUri) { using (var client = new HttpClient()) { return FromYouTubeAsync( () => client .GetStringAsync(videoUri), uri => client .GetByteArrayAsync(uri)); } } public async static Task FromYouTubeAsync( Func<Task> sourceFactory, Func<string, Task> downloadFactory) { string source = await // TaskCanceledException here sourceFactory() .ConfigureAwait(false); // find […]