[CSHARP-2372] Unwatch/stop a change stream Created: 30/Aug/18 Updated: 27/Oct/23 Resolved: 04/Jan/19 |
|
| Status: | Closed |
| Project: | C# Driver |
| Component/s: | Read Operations |
| Affects Version/s: | 2.5 |
| Fix Version/s: | None |
| Type: | Task | Priority: | Major - P3 |
| Reporter: | daniel moqvist | Assignee: | Robert Stam |
| Resolution: | Works as Designed | Votes: | 0 |
| Labels: | question | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Environment: |
Windows |
||
| Description |
|
I cannot find a proper way to cancel a change stream watcher.
Console output:
In the following example I get the correct exception, but the MoveNext method returns true every second and I have to check if the cursor has any document.
Console output:
Here is an example how to cancel a file download using the .net WebClient. The same pattern could fit the cancellation of a change stream watcher.
|
| Comments |
| Comment by Robert Stam [ 04/Jan/19 ] | |||||||
|
You can abort an ongoing Watch by passing a cancellationToken to the ToEnumerable method. The following example aborts the Watch after 10 seconds:
You don't have to use a timer (that's just for this example). You can call cts.Cancel() from any thread to request cancellation of the Watch. Note: the original call to Watch also has an optional cancellationToken parameter. The cancellationToken passed to the Watch method only applies to the first call to the server to begin the Watch operation. You can of course pass the same cancellationToken to both the Watch and ToEnumerable methods. When cancellation is requested via the cancellationToken an OperationCanceledException will be thrown.
|