How to provide timeout to a method call

Posted on 2006-11-27
Medium Priority
Last Modified: 2008-02-01

I have an 3rd party API written in unmanaged C++ which is basically a proxy - it connects to an app server on the internet, receives data and raises events. My application (written in C#) uses this functionality through wrapper class written in managed C++.

Due to bugs in this API, I need to be able to detect if the call is stuck. Ideally, I would have something like this embedded in my wrapper class:

   res = TimedAPIMethod(parameters, timeout);
catch (MyTimeoutException *ex)
// timeout happened, do whatever is needed

where TimedAPIMethod calls API method and provides results or throws a custom exception
if it times out.

I looked into delegates and BeginInvoke/EndInvoke but it seems not to work for me as EndInvoke must be called and I'll get stuck again waiting for delegate to finish.

I had another app where this was handled through separate worker thread launch, for each API call, which would be terminated if not finished in specified time. This doesn't look satisfactory to me beacuse it is not cheap - depending on traffic on app server, various threads in client app might launch multiple calls to API methods per second.

I'd like to hear from anyone who faced similar problems - what did you try, what did you settle for, what are the performance issues if I did this or that.
Question by:cxdood
  • 2
  • 2

Expert Comment

ID: 18019155
in my opnion, unless your 3rd party api is asynchronous , you can't get a timeout exception when you call it.
even you has a working thread, it can't affect the thread you call your api.
you should use event or other method to resolve it.

although, maybe i have a acceptable solution for your problem.
1.define timer task or similar object, 1 task has a unique id and a timeout time(whatevet format)
2.create a task queue, FIFO(first in, first out)
3.create a working thread, to exam task queue.
4.when a new call made, create a new timer task , push to tail of queue.
5.for working thread, continuous check the task  head of queue, if timeout time > now, then wait.
if timeout time <= now, pop the head task, process timeout event, then check next task.

as a result, you can use a single thread to handle all system timeout,
we use this in telecom system for years, hope helpful:)

Author Comment

ID: 18019363
I don't know the details of API implementation but we can treat it as synchronous.

Though I appreciate your idea it would introduce more complexity than benefits to my app.

The way app works is really complicated - it performs live synchronization between remote server application and local SQL Server database. Due to the structure of API and different modes of operation API provides, it has to use two APIWrapper class instances in two different modes, and then use multiple threads for different message queues, because some messages require lenghty processing before insertion into database and some other, much more frequent, do not.

This limits my enthusiasm regarding use of class level (not-method-local) variables I might use to pass parameters to worker threads. After spending a lot of time tidying out the design, synchronization, corruption detection and like, I hit the last wall - the situation where my call to API method just hangs.

Now, my APIWrapper class has a LOT of methods. Modifying all these will definitely be a pain in the ass, and I am tempted to go the easy route of detecting which message pump thread in this particular application is stuck and recreating it. Before I do that, I wanted to see if anyone facing similar problem came up with a better idea, namely encapsulating this functionality in the API wrapper, and not modifying each and every application that relies on this API (and I have a LOT of these too :( ).

BTW, I saw a recently posted article here


that makes use of Begin/EndInvoke but I find it a bit suspicious, since everyone else says EndInvoke is a must. Anyone knows for sure if this can be done like this?

Accepted Solution

Akumas earned 750 total points
ID: 18020092
from my own understanding, begin/end invoke just a .net warper, one working thread, and another thread waiting for
 complete/timeout signal(invoked by WaitHandle.WaitOne).
that's why you should use EndInvoke to wait working thread to finish , or it will cause unpredictable problems.
although the cost is not cheep, maybe you can try callback delegete instead of directly using EndInvoke in main thread,
or create a new thread to hang by EndInvoke:)
at least, your main thread won't block by it~

Author Comment

ID: 18026859

I did some more research on the issue, and after this


it seems that there is practically no way to safely kill a thread stuck in unmanaged call. Guess I'll just have to create a watchdog app and restart the whole app that times out.

Oh well.

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
Integration Management Part 2
Stellar Phoenix SQL Database Repair software easily fixes the suspect mode issue of SQL Server database. It is a simple process to bring the database from suspect mode to normal mode. Check out the video and fix the SQL database suspect mode problem.
Suggested Courses
Course of the Month9 days, 5 hours left to enroll

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question