- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个控制台应用程序,我在其中使用 TPL,当我在任务中点击 whenany 时它会退出。我是线程的新手,有人可以指导我正确的方向吗(我单独执行了 RunJob,它没有异常(exception))。 .所以我不确定如何从这里进行调试。
代码如下:
// Program.cs
static void Main(string[] args)
{
TaskHelper helper = new TaskHelper();
helper.StartProcessing();
}
// TaskHelper Class
public async void StartProcessing()
{
var tasks = new List<Task<bool>>();
int taskNum = _queueList.Count < maxThreads ? _queueList.Count : maxThreads;
for (int i = 0; i < taskNum; i++)
{
UCMDo doObj;
if (_taskQueue.TryDequeue(out doObj))
{
tasks.Add(RunOps(doObj));
}
}
while (tasks.Count > 0)
{
try
{
// Program exits here when its hitting WhenAny line
var t = await Task.WhenAny(tasks);
tasks.Remove(t);
await t;
}
catch (OperationCanceledException)
{
}
catch (Exception exc) { }
finally
{
// add to tasks, and RunOps
}
}
}
async Task<bool> RunOps(UCMDo doJ)
{
var result = await Task.Run(() => UCMFactory.RunJob(_job, doJ));
return result;
}
最佳答案
发生的事情是您调用 StartProcessing
而不等待它(您不能,因为它是 async void
),因此程序到达 Main< 的末尾
并在 StartProcessing
中的操作仍在运行时结束。
StartProcessing
应该返回一个任务,您应该等待该任务完成。这通常是通过等待任务(即 await helper.StartProcessing()
)来完成的,但是由于您不能在 Main
中使用 await
,所以您应该这样做同步的(尽管这是唯一可以接受的地方):
static void Main(string[] args)
{
TaskHelper helper = new TaskHelper();
helper.StartProcessingAsync().Wait();
}
正如 Servy 正确指出的那样,一个更健壮和生产就绪的解决方案是使用某种消息循环。一个例子是 Stephen Cleary's AsyncContext
:
static void Main(string[] args)
{
TaskHelper helper = new TaskHelper();
AsyncContext.Run(() => helper.StartProcessingAsync());
}
注意事项:
async void
永远不应在 UI 事件处理程序之外使用async
方法通常应命名为 XAsync
(即 StartProcessingAsync
)关于c# - Whenany() 在任务列表中退出程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28284222/
我有一个控制台应用程序,我在其中使用 TPL,当我在任务中点击 whenany 时它会退出。我是线程的新手,有人可以指导我正确的方向吗(我单独执行了 RunJob,它没有异常(exception))。
当将多个已完成的任务传递给 Task.WhenAny 时,Task.WhenAny 会优先选择将返回哪个已完成的任务? 最佳答案 当您想知道确切的行为时,通常可以查看 reference source
考虑一个具有某种生命周期的类。在此生命周期中,一个事件可能会发生任意多次,并且该事件是通过完成任务(在事件发生后更新)来发出信号的。该对象也可能被关闭,结束其生命周期。关闭也通过完成任务发出信号。它也
我有这段代码: var client = new TcpClient(); HttpRequestInfo.AddTimestamp("Connecting"); await Task.WhenAny
我在 ReactiveLists 中保存了一些检查列表,它们具有 ChangeTrackingEnabled = true。我只想在每个列表中至少选中一个项目时才启用我的 OkCommand。 此外,
在下面的代码中: if (await Task.WhenAny(task, Task.Delay(100)) == task) { success = true
我想同时执行多个异步任务。每个任务将运行一个 HTTP 请求,该请求可以成功完成或引发异常。我需要等待直到第一个任务成功完成,或者直到所有任务都失败。 如何实现 Task.WhenAny 的重载接受谓
当我使用 Task.WhenAll() 函数并在任务中抛出异常时,会抛出一个新的 AggregateException,我可以捕获它以查看任务中发生的所有异常。但是,当我使用 Task.WhenAny
在我的应用程序中,我正在创建一些并发的网络请求,当其中任何一个完成时我都很满意,所以我使用了方法 Task.WhenAny : var urls = new string[] { "https
假设我有三个任务,a、b 和 c。这三个都保证在 1 到 5 秒之间的随机时间抛出异常。然后我写了下面的代码: await Task.WhenAny(a, b, c); 这最终会从最先出错的任务中抛出
我有以下代码: List> tasks = tasksQuery.ToList(); while (tasks.Any()) { Task completedTask = await Task
我需要为移动应用程序中的任务调用添加超时功能。我尝试使用 Task.WhenAny 来完成此操作,如下所示。这将返回首先完成的任务。我的问题是,最初我是从这个任务中获取返回值的,如果任务没有超时,我仍
我有以下代码片段来处理 Azure 通知中心推送通知: var alert = "{\"aps\":{\"alert\":\"" + message + "\"}}"; var task = Azur
我有一个带有两个 WhenAnyValues 的对象: this.WhenAnyValue(x => x.SomeInt) .Select(x => "Bar" + x) .ToPro
我的解决方案中有两个项目:WPF 项目和类库。 在我的类库中: 我有一个符号列表: class Symbol { Identifier Identifier {get;set;}
我有一个带有两个 WhenAnyValues 的对象: this.WhenAnyValue(x => x.SomeInt) .Select(x => "Bar" + x) .ToPro
我如何并行运行 2 个任务并在一个特定任务结束时得到通知(在我的例子中是列表中的第一个任务) 目前我正在使用这段代码: await Task.WhenAny(A.Play(cancel), B.Pla
我有一个创建多个基于 I/O 的任务的解决方案,我正在使用 Task.WhenAny() 来管理这些任务。但往往很多任务会因为网络问题或请求节流等原因而失败。 在使用 Task.WhenAny() 方
考虑在 ReactiveUI 中使用继承。我有带有 DoSomethingCommand 的 ViewModel 基类。此命令的“CanExecute”取决于属性 Prop1 public class
Reactive UI 中有几种扩展方法可用于获取属性更改的 observable。 我想我明白 WhenAny和 WhenAnyValue . WhenAny用于一系列属性更改通知,您希望其中的对象
我是一名优秀的程序员,十分优秀!