Search code examples
.netfunctiontimeoutdropboxsharpbox

Set a timeout value on calling a function in .NET


Is it possible to set a timeout value when calling a function in .NET? I am using a third party component called SharpBox that loops a set of Dropbox accounts and pulls in the contents to a given folder on my server.

Dim itemsDownload as integer = pollSingleDropboxAccount(accountID)

function pollSingleDropboxAccount(accountID as integer) as integer
   //Utilises Sharpbox to download the files and return the count
   //In here a connection is established using the SharpBox.dll
   //but never gets closed or throws an exception leaving the
   //function to hang indefinitely
end function

Sharpbox uses a HTTPWebRequest behind the scenes to contact the Dropbox API but seems to default the timeout to infinite and this value cannot be overridden. I've lodged a feature request with SharpBox but I'm looking for something we can use in the mean time as a workaround.

To circumvent this I'm wondering if we can wrap something around the call to the 'pollSingleDropboxAccount' function to give up after say 5 minutes?


Solution

  • Yes, it's possible.

    1) Create and run timer after the function call and start it

    2) In timer's callback do whatever you need (call your function)

    Read about timer on MSDN

    EDIT: Note, that there are at least 3 types of timer in .NET and they behave different. Some of them will fire after the timeout (every 5 minutes in your case) until you call Stop on them and some of them are executed only once.