Terry
Terry

Reputation: 23

Async long running process

I have a question about a scenario I need to code. I have a process that will send a request file via SFTP and then I will need to wait for a response file to be created on the ftp server before I download and continue my processing. The response file could take anywhere between 1min to 60mins to appear. If I await this process, is there a way that I can say, give up after 60 mins? C#

Upvotes: 1

Views: 153

Answers (1)

Karen Payne
Karen Payne

Reputation: 5102

Use a CancellationTokenSource

private CancellationTokenSource _cancellationTokenSource =
    new CancellationTokenSource(TimeSpan.FromMinutes(60));

Wrap the code in a try/catch and watch for TaskCanceledException

Also, check out Asynchronously wait for Task to complete with timeout

Upvotes: 1

Related Questions