我当前正在构建MVVM应用程序,我的一个视图模型使用已注册依赖项注入的服务。该服务针对各种第三方应用程序运行powershell cmdlet或http REST命令,当它们同时收到多个请求时,他们对此并不满意。
这就是为什么我希望能够从UI触发几个操作(而不阻止它),但要确保该服务一次只能处理一个。如果我的UI元素正在工作或正在等待,则会同时显示。
我试图实现TPL ActionBlock,但到目前为止,我发现所有操作都同时运行,这是我发现使它们在队列中工作的唯一方法,它将阻塞UI,直到所有任务完成。
这是我所做的:
我的视图模型包含一个ObservableCollection元素,其中包含两个列表(一个嵌套在另一个列表中)。在UI上,它看起来像一个项目列表,可以展开以显示小树视图。
我想要的是,每次我扩展一个项目时,树状视图中的所有子项目都要通过该服务检查其在第三方应用程序中的状态。 UI子项中的方法如下:
private async Task<bool> UpdateSubItemsStatus()
{
foreach (var item in connectorsMenuItems)
{
await parent.Library.EnqueueConnectorOperations(Connectors.service.OperationType.CheckPresence, parent.CI, AssetId, item.ConnectorID, parent.ConnectorInterfaces.Single(c => c.ItemId == AssetId).ItemsConnectorPresence.Single(i => i.ConnectorId == item.ConnectorID));
}
return true;
}
在这里,“ parent”是第一级项目,“ parent.Library”是托管所有内容的主视图模型。
在View模型上,检索此方法的方法如下:
public async Task EnqueueConnectorOperations(OperationType operationType, ConfigurationItem ci, Guid itemId, Guid ConnectorID, ItemConnectorPresence itemPresence)
{
logManager.WriteLog($"Library : Received connector operation for item {itemId}, the name of the item is {itemPresence.ItemName}", System.Threading.Thread.CurrentThread.ManagedThreadId.ToString(), LogManagement.LogLevel.Information);
//Set requestor UI item in working state in the UI
if(ci.CIType == EnumCIType.Application)
{
LibraryItems.Single(l => l.CI.ID == ci.ID).DeployableAssetMenuItems.Single(d => d.AssetId == itemId).ConnectorsMenuItems.Single(c => c.ConnectorID == ConnectorID).IsWorking = true;
LibraryItems.Single(l => l.CI.ID == ci.ID).DeployableAssetMenuItems.Single(d => d.AssetId == itemId).ConnectorsMenuItems.Single(c => c.ConnectorID == ConnectorID).Status = LibraryItemState.UpdatingStatus;
LibraryItems.Single(l => l.CI.ID == ci.ID).DeployableAssetMenuItems.Single(d => d.AssetId == itemId).ConnectorsMenuItems.Single(c => c.ConnectorID == ConnectorID).StatusString = "Checking Presence";
}
ActionBlock<OperationType> actionBlock = new ActionBlock<OperationType>(async _operationType =>
{
logManager.WriteLog($"Library : Sending the operation to connector service : item {itemId}, the name of the item is {itemPresence.ItemName}", System.Threading.Thread.CurrentThread.ManagedThreadId.ToString(), LogManagement.LogLevel.Information);
await connectorService.EnqueueConnectorOperations(operationType, ci, itemId, Settings.Default.ProjectLocalPath + @"\" + ci.ID.ToString(), ConnectorID, Settings.Default.DisplayLanguage, Settings.Default.ProjectLocalPath, itemPresence).ConfigureAwait(false);
}, new ExecutionDataflowBlockOptions
{
MaxDegreeOfParallelism = 1,
CancellationToken = new CancellationTokenSource(TimeSpan.FromMinutes(5)).Token,
});
actionBlock.Post(operationType);
actionBlock.Complete();
actionBlock.Completion.Wait();
}
然后在这里命名为“ connectorService”的服务完成其工作。
如果我使用actionBlock.Completion.Wait(),则在最后一行中,所有任务均按顺序运行,但我的UI被阻止。
如果我改用等待actionBlock.Completion()。用户界面没有被阻止,但全部并行运行。
因此,如果有人提出建议,那就太好了!
更新:
我根据自己的需要调整了JSteward的anerser:
我按照您的建议声明了ActionBlock作为我的视图模型的私有成员。但是,当我按照您在支出一项时所说的那样进行操作时,它在正确排队的地方是可操作的,但是如果我扩展另一项,那么它的操作(也在他们的队列中)将与第一项的操作并行运行。无论要求多少项目,我都不希望一次只执行一项操作。
因此,我进行了以下更改: 全部在视图模型的构造函数中初始化ActionBlock:
public ViewModelCtor()
{
actionBlock = new ActionBlock<ConnectorOperationArgWrapper>(async _connectorOperationArgWrapper =>
{
logManager.WriteLog($"Library : Sending the operation to connector service for {_connectorOperationArgWrapper.itemPresence.ItemName} on connector {connectorService.GetConnectorName(_connectorOperationArgWrapper.itemPresence.ConnectorId)}", System.Threading.Thread.CurrentThread.ManagedThreadId.ToString(), LogLevel.Information);
LibraryItems.Single(l => l.CI.ID == _connectorOperationArgWrapper.ci.ID).DeployableAssetMenuItems.Single(d => d.AssetId == _connectorOperationArgWrapper.itemPresence.itemId).ConnectorsMenuItems.Single(c => c.ConnectorID == _connectorOperationArgWrapper.itemPresence.ConnectorId).StatusString = "Cheking Presence";
LibraryItems.Single(l => l.CI.ID == _connectorOperationArgWrapper.ci.ID).DeployableAssetMenuItems.Single(d => d.AssetId == _connectorOperationArgWrapper.itemPresence.itemId).ConnectorsMenuItems.Single(c => c.ConnectorID == _connectorOperationArgWrapper.itemPresence.ConnectorId).Status = LibraryItemState.UpdatingStatus;
await connectorService.EnqueueConnectorOperations(_connectorOperationArgWrapper.operationType, _connectorOperationArgWrapper.ci, _connectorOperationArgWrapper.itemPresence.itemId, Settings.Default.ProjectLocalPath + @"\" + _connectorOperationArgWrapper.ci.ID.ToString(), _connectorOperationArgWrapper.itemPresence.ConnectorId, Settings.Default.DisplayLanguage, Settings.Default.ProjectLocalPath, _connectorOperationArgWrapper.itemPresence).ConfigureAwait(false);
}, new ExecutionDataflowBlockOptions
{
MaxDegreeOfParallelism = 1,
});
}
所以按现在扩展的项调用的方法看起来像这样:
public async Task EnqueueConnectorOperations(ConnectorOperationArgWrapper _args)
{
logManager.WriteLog($"Library : Received operation request for {_args.itemPresence.ItemName} on connector {connectorService.GetConnectorName(_args.itemPresence.ConnectorId)}", System.Threading.Thread.CurrentThread.ManagedThreadId.ToString(), LogLevel.Information);
if (_args.ci.CIType == EnumCIType.Application)
{
LibraryItems.Single(l => l.CI.ID == _args.ci.ID).DeployableAssetMenuItems.Single(d => d.AssetId == _args.itemPresence.itemId).ConnectorsMenuItems.Single(c => c.ConnectorID == _args.itemPresence.ConnectorId).IsWorking = true;
LibraryItems.Single(l => l.CI.ID == _args.ci.ID).DeployableAssetMenuItems.Single(d => d.AssetId == _args.itemPresence.itemId).ConnectorsMenuItems.Single(c => c.ConnectorID == _args.itemPresence.ConnectorId).Status = LibraryItemState.NeedsAttention;
LibraryItems.Single(l => l.CI.ID == _args.ci.ID).DeployableAssetMenuItems.Single(d => d.AssetId == _args.itemPresence.itemId).ConnectorsMenuItems.Single(c => c.ConnectorID == _args.itemPresence.ConnectorId).StatusString = "Waiting";
}
logManager.WriteLog($"Library : post actionblock", System.Threading.Thread.CurrentThread.ManagedThreadId.ToString(), LogLevel.Information);
await actionBlock.SendAsync(_args);
//actionBlock.Complete();
//await actionBlock.Completion;
}
我评论了具有actionBlock完成和完成的部分,因为我希望该块能够在任何时间甚至每个项目多次接收和排队请求。
到目前为止,它似乎可行,是否像我以前那样做正确,还是我将为此遇到一些麻烦?
答案 0 :(得分:1)
现在,您将为每个操作创建一个新的ActionBlock
。 ActionBlock
有一个内部队列,您应该将内部消息发送到该队列,并使其使用单个ActionBlock
顺序运行它们。通过重新安排事情并使ActionBlock
成为类成员,您将可以更好地控制它并等待每组子视图项。
private ActionBlock<OperationType> actionBlock;
public void OnTreeViewExpand()
{
//Re-initialize the actionblock for a new set of operations
actionBlock = new ActionBlock<OperationType>(async _operationType =>
{
logManager.WriteLog($"Library : Sending the operation to connector service : item {itemId}, the name of the item is {itemPresence.ItemName}", System.Threading.Thread.CurrentThread.ManagedThreadId.ToString(), LogManagement.LogLevel.Information);
await connectorService.EnqueueConnectorOperations(operationType, ci, itemId, Settings.Default.ProjectLocalPath + @"\" + ci.ID.ToString(), ConnectorID, Settings.Default.DisplayLanguage, Settings.Default.ProjectLocalPath, itemPresence).ConfigureAwait(false);
}, new ExecutionDataflowBlockOptions
{
MaxDegreeOfParallelism = 1,
CancellationToken = new CancellationTokenSource(TimeSpan.FromMinutes(5)).Token,
});
}
private async Task<bool> UpdateSubItemsStatus()
{
foreach (var item in connectorsMenuItems)
{
await parent.Library.EnqueueConnectorOperations(Connectors.service.OperationType.CheckPresence, parent.CI, AssetId, item.ConnectorID, parent.ConnectorInterfaces.Single(c => c.ItemId == AssetId).ItemsConnectorPresence.Single(i => i.ConnectorId == item.ConnectorID));
}
//All items sent, signal completion
actionBlock.Complete();
await actionBlock.Completion;
return true;
}
public Task EnqueueConnectorOperations(OperationType operationType, ConfigurationItem ci, Guid itemId, Guid ConnectorID, ItemConnectorPresence itemPresence)
{
logManager.WriteLog($"Library : Received connector operation for item {itemId}, the name of the item is {itemPresence.ItemName}", System.Threading.Thread.CurrentThread.ManagedThreadId.ToString(), LogManagement.LogLevel.Information);
//Set requestor UI item in working state in the UI
if (ci.CIType == EnumCIType.Application)
{
LibraryItems.Single(l => l.CI.ID == ci.ID).DeployableAssetMenuItems.Single(d => d.AssetId == itemId).ConnectorsMenuItems.Single(c => c.ConnectorID == ConnectorID).IsWorking = true;
LibraryItems.Single(l => l.CI.ID == ci.ID).DeployableAssetMenuItems.Single(d => d.AssetId == itemId).ConnectorsMenuItems.Single(c => c.ConnectorID == ConnectorID).Status = LibraryItemState.UpdatingStatus;
LibraryItems.Single(l => l.CI.ID == ci.ID).DeployableAssetMenuItems.Single(d => d.AssetId == itemId).ConnectorsMenuItems.Single(c => c.ConnectorID == ConnectorID).StatusString = "Checking Presence";
}
return actionBlock.SendAsync(operationType);
}
答案 1 :(得分:1)
将BlockingCollection与TaskCompletionSource一起使用
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Reactive;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Threading.Tasks;
namespace test
{
class Program
{
class WorkItem
{
public int Id { get; set; } // you can make it store more things
public TaskCompletionSource<DateTime> TaskSource { get; } = new TaskCompletionSource<DateTime>();
}
class Worker : IDisposable
{
private BlockingCollection<WorkItem> _queue;
private Task _consumer;
public Worker()
{
_queue = new BlockingCollection<WorkItem>();
_consumer = Task.Run(async () =>
{
foreach (var item in _queue.GetConsumingEnumerable())
{
await Task.Delay(1000); // some hard work
item.TaskSource.TrySetResult(DateTime.Now); // try is safer
// you can return whatever you want
}
});
}
public Task<DateTime> DoWork(int i) // return whatever you want
{
var workItem = new WorkItem { Id = i };
_queue.Add(workItem);
return workItem.TaskSource.Task;
}
public void Dispose()
{
_queue.CompleteAdding();
}
}
public static void Main(string[] args)
{
using (var worker = new Worker())
{
Task.Run(async () =>
{
var tasks = Enumerable.Range(0,10).Select(x => worker.DoWork(x)).ToArray();
var time = await tasks[1];
Console.WriteLine("2nd task finished at " + time);
foreach (var task in tasks)
{
time = await task;
Console.WriteLine("Task finished at " + time);
}
Console.ReadLine();
}).Wait();
}
}
}
}
// output
2nd task finished at 2019-01-22 19:14:57
Task finished at 2019-01-22 19:14:56
Task finished at 2019-01-22 19:14:57
Task finished at 2019-01-22 19:14:58
Task finished at 2019-01-22 19:14:59
Task finished at 2019-01-22 19:15:00
Task finished at 2019-01-22 19:15:01
Task finished at 2019-01-22 19:15:02
Task finished at 2019-01-22 19:15:03
Task finished at 2019-01-22 19:15:04
Task finished at 2019-01-22 19:15:05
这使您可以轻松地等待命令中的单个项目,例如无需阻塞UI线程。