Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
2.4 KiB

  1. #ifndef _RPCTIMEOUT_H_
  2. #define _RPCTIMEOUT_H_
  3. ////////////////
  4. //
  5. // Timing out remote calls - Use this if you are using an out-of-proc
  6. // object (e.g., a clipboard data object) that may belong to an app
  7. // that is hung.
  8. //
  9. // If any individual method takes more than dwTimeout milliseconds, the
  10. // call will be aborted and you will get an error code back.
  11. //
  12. // Usage:
  13. //
  14. // Typical usage...
  15. //
  16. // CRPCTimeout timeout; // optional timeout in milliseconds
  17. // hr = pdto->GetData(...); // make some remote call
  18. // hr = pdto->GetData(...); // make another remote call
  19. //
  20. // If either of the GetData calls takes more than TIMEOUT_DEFAULT
  21. // milliseconds, it will be cancelled and return an error.
  22. //
  23. // Timed-out-ness is sticky. Once a single call has timed out, all
  24. // subsequent calls will be timed out immediately (to avoid hanging
  25. // on the same server over and over again) until the timeout object
  26. // is re-armed.
  27. //
  28. // When the CRPCTimeout goes out of scope, it will disarm itself.
  29. // Or you can explicitly call the Disarm() method.
  30. //
  31. // Fancier usage...
  32. //
  33. // CRPCTimeout timeout(5000); // five seconds
  34. // hr = pdto->GetData(); // this one times out after 5 seconds
  35. // timeout.Disarm(); // disable the timeout
  36. // hr = pdto->GetData(); // this one runs as long as necesary
  37. // timeout.Arm(2000); // rearm the timer with a new timeout
  38. // hr = pdto->GetData(); // this one times out after 2 seconds
  39. // hr = pdto->GetData(); // this one times out after 2 seconds
  40. // if (timeout.TimedOut()) ... // handle the timeout scenario
  41. //
  42. //
  43. //
  44. // If you create multiple CRPCTimeout objects, you MUST disarm them in
  45. // reverse order or the timeout chain will be corrupted. (Debug-only
  46. // code will attempt to catch this bug.)
  47. //
  48. // Instead of creating multiple timeout objects at the same scope, you
  49. // should create just one object and rearm it.
  50. //
  51. //
  52. class CRPCTimeout {
  53. public:
  54. CRPCTimeout() { Init(); Arm(); }
  55. CRPCTimeout(DWORD dwTimeout) { Init(); Arm(dwTimeout); }
  56. ~CRPCTimeout() { Disarm(); }
  57. void Init();
  58. void Arm(DWORD dwTimeout = 0);
  59. void Disarm();
  60. BOOL TimedOut() const { return _fTimedOut; }
  61. private:
  62. static void CALLBACK _Callback(PVOID lpParameter, BOOLEAN);
  63. DWORD _dwThreadId;
  64. BOOL _fTimedOut;
  65. HRESULT _hrCancelEnabled;
  66. HANDLE _hTimer;
  67. };
  68. #endif // _RPCTIMEOUT_H_