<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Cancelling ListFolderLongpollAsync in Dropbox API Support &amp; Feedback</title>
    <link>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Cancelling-ListFolderLongpollAsync/m-p/269223#M15875</link>
    <description>System.Diagnostics.Debug.WriteLine ("Task finished"); does not actually get invoked in the second code snippet.</description>
    <pubDate>Tue, 20 Mar 2018 02:32:30 GMT</pubDate>
    <dc:creator>xtremebytes</dc:creator>
    <dc:date>2018-03-20T02:32:30Z</dc:date>
    <item>
      <title>Cancelling ListFolderLongpollAsync</title>
      <link>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Cancelling-ListFolderLongpollAsync/m-p/269222#M15874</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a Dropbox longpoll running as a task, implemented in C# (Xamarin Forms) as the following.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;async Task DropboxLongpoll (string cursor, CancellationToken token)
		{
			var longpollClient = this.GetClient ();
			int backoff = 0;
			System.Diagnostics.Debug.WriteLine ("Running background longpoll task.");
			while (!token.IsCancellationRequested) {
				var longpollResults = await longpollClient.Files.ListFolderLongpollAsync (cursor); //default timeout is 30 seconds
				System.Diagnostics.Debug.WriteLine ("Changes: " + longpollResults.Changes + " Backoff: " + longpollResults.Backoff.GetValueOrDefault ());
				if (longpollResults.Changes) {
					var folderResult = await longpollClient.Files.ListFolderContinueAsync (cursor);
					cursor = folderResult.Cursor;
					DropboxLongpollReportChanges (folderResult);
				}
				//backoff if present
				if (longpollResults.Backoff != null) {
					backoff = (int)longpollResults.Backoff.GetValueOrDefault ();
				}
				System.Diagnostics.Debug.WriteLine ("Backing off longpoll for " + backoff + " seconds");
				await Task.Delay (backoff * 1000); //milliseconds delay
			}
			System.Diagnostics.Debug.WriteLine ("Cancelling task");
			longpollClient.Dispose ();
			token.ThrowIfCancellationRequested ();
			System.Diagnostics.Debug.WriteLine ("Task finished");
		}&lt;/PRE&gt;
&lt;P&gt;While I can nicely cancel (i.e., the while loop stops) this task by calling Cancel on the CancellationTokenSource from which I got the token, the Dropbox longpoll task seems to keep running, i.e., listening for changes. I can ignore the changes it reports by modifying this code to the following.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;async Task DropboxLongpoll (string cursor, CancellationToken token)
		{
			var longpollClient = this.GetClient ();
			int backoff = 0;
			System.Diagnostics.Debug.WriteLine ("Running background longpoll task.");
			while (!token.IsCancellationRequested) {
				var longpollResults = await longpollClient.Files.ListFolderLongpollAsync (cursor); //default timeout is 30 seconds
                if (!token.IsCancellationRequested) { //check if the cancellation is still not requested
				System.Diagnostics.Debug.WriteLine ("Changes: " + longpollResults.Changes + " Backoff: " + longpollResults.Backoff.GetValueOrDefault ());
				if (longpollResults.Changes) {
					var folderResult = await longpollClient.Files.ListFolderContinueAsync (cursor);
					cursor = folderResult.Cursor;
					DropboxLongpollReportChanges (folderResult);
				}
				//backoff if present
				if (longpollResults.Backoff != null) {
					backoff = (int)longpollResults.Backoff.GetValueOrDefault ();
				}
				System.Diagnostics.Debug.WriteLine ("Backing off longpoll for " + backoff + " seconds");
				await Task.Delay (backoff * 1000); //milliseconds delay
				} else {
					System.Diagnostics.Debug.WriteLine ("Cancelling task");
					longpollClient.Dispose ();
					token.ThrowIfCancellationRequested ();
				}
			}
			System.Diagnostics.Debug.WriteLine ("Task finished");
		}&lt;/PRE&gt;
&lt;P&gt;But, this is just ignoring the detected changes. I believe that the long poll will keep running as a background thread. Will the Dropbox longpoll async task actually cancel? Is there a way to pass the token to the&amp;nbsp;ListFolderLongpollAsync so that it actually cancel when cancellation is requested?&lt;/P&gt;</description>
      <pubDate>Wed, 29 May 2019 09:14:34 GMT</pubDate>
      <guid>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Cancelling-ListFolderLongpollAsync/m-p/269222#M15874</guid>
      <dc:creator>xtremebytes</dc:creator>
      <dc:date>2019-05-29T09:14:34Z</dc:date>
    </item>
    <item>
      <title>Re: Cancelling ListFolderLongpollAsync</title>
      <link>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Cancelling-ListFolderLongpollAsync/m-p/269223#M15875</link>
      <description>System.Diagnostics.Debug.WriteLine ("Task finished"); does not actually get invoked in the second code snippet.</description>
      <pubDate>Tue, 20 Mar 2018 02:32:30 GMT</pubDate>
      <guid>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Cancelling-ListFolderLongpollAsync/m-p/269223#M15875</guid>
      <dc:creator>xtremebytes</dc:creator>
      <dc:date>2018-03-20T02:32:30Z</dc:date>
    </item>
    <item>
      <title>Re: Cancelling ListFolderLongpollAsync</title>
      <link>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Cancelling-ListFolderLongpollAsync/m-p/269224#M15876</link>
      <description>&lt;P&gt;The line&amp;nbsp;&lt;FONT face="Menlo"&gt; &lt;SPAN style="color: #789769;"&gt;token.ThrowIfCancellationRequested&amp;nbsp;();&lt;/SPAN&gt;&lt;/FONT&gt; was not necessary. I realised there is a different reason why the longpoll seemed to be called twice. I had a threading issue with the&amp;nbsp;&lt;FONT face="Menlo"&gt; &lt;SPAN style="color: #eeeeec;"&gt;DropboxLongpollReportChanges()&lt;/SPAN&gt;&lt;/FONT&gt; method. Still, it would be nice to know if the longpoll task is actually getting cancelled so that it doesn't waste resources.&lt;/P&gt;</description>
      <pubDate>Tue, 20 Mar 2018 02:45:28 GMT</pubDate>
      <guid>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Cancelling-ListFolderLongpollAsync/m-p/269224#M15876</guid>
      <dc:creator>xtremebytes</dc:creator>
      <dc:date>2018-03-20T02:45:28Z</dc:date>
    </item>
    <item>
      <title>Re: Cancelling ListFolderLongpollAsync</title>
      <link>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Cancelling-ListFolderLongpollAsync/m-p/269306#M15885</link>
      <description>It's not possible to cancel an ongoing request instantly unfortunately, but the long poll request will eventually close or time out on its own. I'll pass this along as a feature request for actual CancellationToken support in the SDK though.</description>
      <pubDate>Tue, 20 Mar 2018 15:47:23 GMT</pubDate>
      <guid>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Cancelling-ListFolderLongpollAsync/m-p/269306#M15885</guid>
      <dc:creator>Greg-DB</dc:creator>
      <dc:date>2018-03-20T15:47:23Z</dc:date>
    </item>
    <item>
      <title>Re: Cancelling ListFolderLongpollAsync</title>
      <link>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Cancelling-ListFolderLongpollAsync/m-p/269360#M15890</link>
      <description>Thanks a lot!</description>
      <pubDate>Tue, 20 Mar 2018 21:23:11 GMT</pubDate>
      <guid>https://www.dropboxforum.com/t5/Dropbox-API-Support-Feedback/Cancelling-ListFolderLongpollAsync/m-p/269360#M15890</guid>
      <dc:creator>xtremebytes</dc:creator>
      <dc:date>2018-03-20T21:23:11Z</dc:date>
    </item>
  </channel>
</rss>

