gpt4 book ai didi

c# - 使用 CancellationToken 中断嵌套任务

转载 作者:行者123 更新时间:2023-11-30 15:17:29 29 4
gpt4 key购买 nike

这是我的场景:用户单击 WPF 按钮,该按钮启动 map 上点集合的开放时间段。当用户单击“完成收集”按钮时,我希望 CollectPoints() 任务完成。

这是我的 SegmentRecorder 类的片段:

    private CancellationTokenSource _cancellationToken;     

public virtual async void RecordSegment(IRoadSegment segment)
{
_cancellationToken = new CancellationTokenSource();
var token = _cancellationToken.Token;

// await user clicking points on map
await CollectPoints(token);

// update the current segment with the collected shape.
CurrentSegment.Shape = CurrentGeometry as Polyline;
}

// collect an arbitrary number of points and build a polyline.
private async Task CollectPoints(CancellationToken token)
{
var points = new List<MapPoint>();
while (!token.IsCancellationRequested)
{
// wait for a new point.
var point = await CollectPoint();
points.Add(point);

// add point to current polyline
var polylineBuilder = new PolylineBuilder(points, SpatialReferences.Wgs84);
CurrentGeometry = polylineBuilder.ToGeometry();

// draw points
MapService.DrawPoints(CurrentGeometry);
}
}

// collect a point from map click.
protected override Task<MapPoint> CollectPoint()
{
var tcs = new TaskCompletionSource<MapPoint>();
EventHandler<IMapClickedEventArgs> handler = null;
handler = (s, e) =>
{
var mapPoint = e.Geometry as MapPoint;
if (mapPoint != null)
{
tcs.SetResult(new MapPoint(mapPoint.X, mapPoint.Y, SpatialReferences.Wgs84));
}
MapService.OnMapClicked -= handler;
};
MapService.OnMapClicked += handler;

return tcs.Task;
}

public void StopSegment(){
// interrupt the CollectPoints task.
_cancellationToken.Cancel();
}

以下是我的 View 模型的相关部分:

public SegmentRecorder SegmentRecorder { get; }
public RelayCommand StopSegment { get; }

public ViewModel(){
StopSegment = new RelayCommand(ExecuteStopSegment);
SegmentRecorder = new SegmentRecorder();
}

// execute on cancel button click.
public void ExecuteStopSegment(){
SegmentRecorder.StopSegment();
}

当我在 while (!token.IsCancellationRequested) 行上放置一个断点并单击取消按钮时,我永远不会到达那个点。

我在这里使用取消 token 的方式是否正确?

最佳答案

CollectPoints 方法将在您调用 后第一次遇到 while 条件 !token.IsCancellationRequested 时返回CancellationTokenSource 的 Cancel() 方法。

while 循环中的代码仍在执行时,任务不会被取消。

正如@JSteward 在他的评论中建议的那样,您应该在 StopSegment() 方法中取消或完成 TaskCompletionSource

像这样:

public virtual async void RecordSegment(IRoadSegment segment)
{
_cancellationToken = new CancellationTokenSource();
var token = _cancellationToken.Token;

// await user clicking points on map
await CollectPoints(token);

// update the current segment with the collected shape.
CurrentSegment.Shape = CurrentGeometry as Polyline;
}

// collect an arbitrary number of points and build a polyline.
private async Task CollectPoints(CancellationToken token)
{
var points = new List<MapPoint>();
while (!token.IsCancellationRequested)
{
try
{
// wait for a new point.
var point = await CollectPoint(token);

//...
}
catch (Exception) { }
}
}

private TaskCompletionSource<MapPoint> tcs;
protected override Task<MapPoint> CollectPoint()
{
tcs = new TaskCompletionSource<MapPoint>();
//...
return tcs.Task;
}

public void StopSegment()
{
// interrupt the CollectPoints task.
_cancellationToken.Cancel();
tcs.SetCanceled();
}

关于c# - 使用 CancellationToken 中断嵌套任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46241799/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com