We are aware of the issue with the badge emails resending to everyone, we apologise for the inconvenience - learn more here.

Forum Discussion

xtremebytes's avatar
xtremebytes
Explorer | Level 3
7 years ago

Cancelling ListFolderLongpollAsync

Hi,

 

I have a Dropbox longpoll running as a task, implemented in C# (Xamarin Forms) as the following.

 

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");
		}

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.

 

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");
		}

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 ListFolderLongpollAsync so that it actually cancel when cancellation is requested?

  • Greg-DB's avatar
    Greg-DB
    7 years ago
    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.
  • xtremebytes's avatar
    xtremebytes
    Explorer | Level 3
    System.Diagnostics.Debug.WriteLine ("Task finished"); does not actually get invoked in the second code snippet.
  • xtremebytes's avatar
    xtremebytes
    Explorer | Level 3

    The line  token.ThrowIfCancellationRequested (); 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  DropboxLongpollReportChanges() method. Still, it would be nice to know if the longpoll task is actually getting cancelled so that it doesn't waste resources.

    • Greg-DB's avatar
      Greg-DB
      Icon for Dropbox Staff rankDropbox Staff
      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.

About Dropbox API Support & Feedback

Node avatar for Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

5,877 PostsLatest Activity: 12 months ago
325 Following

If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X or Facebook.

For more info on available support options for your Dropbox plan, see this article.

If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!