Leaked source code of windows server 2003
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.

139 lines
3.1 KiB

  1. //
  2. // asynccal.cpp
  3. //
  4. #include "private.h"
  5. #include "asynccal.h"
  6. //////////////////////////////////////////////////////////////////////////////
  7. //
  8. // CAsyncCall
  9. //
  10. // Some TIP shows the modal dialog box or message box in OnClieck() or
  11. // OnMenuSelected() method. Then tipbar thread got in a dead lock
  12. // status until it returns. To avoid this problem, we crate another thread
  13. // to call OnClick() or OnMenuSelected() method so langBar UI does not
  14. // have to wait for the return.
  15. //
  16. //////////////////////////////////////////////////////////////////////////////
  17. ULONG CAsyncCall::_AddRef( )
  18. {
  19. return InterlockedIncrement(&_ref);
  20. }
  21. ULONG CAsyncCall::_Release( )
  22. {
  23. ULONG cr = InterlockedDecrement(&_ref);
  24. if (cr == 0) {
  25. delete this;
  26. }
  27. return cr;
  28. }
  29. //+---------------------------------------------------------------------------
  30. //
  31. // StartThread
  32. //
  33. //----------------------------------------------------------------------------
  34. HRESULT CAsyncCall::StartThread()
  35. {
  36. HANDLE hThread;
  37. DWORD dwRet;
  38. HRESULT hr = S_OK;
  39. _hr = S_OK;
  40. _AddRef();
  41. _fThreadStarted = FALSE;
  42. hThread = CreateThread(NULL, 0, s_ThreadProc, this, 0, &_dwThreadId);
  43. if (hThread)
  44. {
  45. //
  46. // we need to wait at least ThreadProc() is started.
  47. // Is it takes more than 30s, it terminate the thread.
  48. //
  49. DWORD dwCnt = 60;
  50. while (!_fThreadStarted && dwCnt--)
  51. {
  52. dwRet = WaitForSingleObject(hThread, 500);
  53. }
  54. if (!_fThreadStarted)
  55. {
  56. TerminateThread(hThread, 0);
  57. }
  58. CloseHandle(hThread);
  59. }
  60. hr = _hr;
  61. _Release();
  62. return hr;
  63. }
  64. //+---------------------------------------------------------------------------
  65. //
  66. // ThreadProc
  67. //
  68. //----------------------------------------------------------------------------
  69. DWORD CAsyncCall::s_ThreadProc(void *pv)
  70. {
  71. CAsyncCall *_this = (CAsyncCall *)pv;
  72. return _this->ThreadProc();
  73. }
  74. DWORD CAsyncCall::ThreadProc()
  75. {
  76. HRESULT hr = E_FAIL;
  77. _AddRef();
  78. _fThreadStarted = TRUE;
  79. switch(_action)
  80. {
  81. case DOA_ONCLICK:
  82. if (_plbiButton)
  83. {
  84. hr = _plbiButton->OnClick(_click, _pt, &_rc);
  85. }
  86. else if (_plbiBitmapButton)
  87. {
  88. hr = _plbiBitmapButton->OnClick(_click, _pt, &_rc);
  89. }
  90. else if (_plbiBitmap)
  91. {
  92. hr = _plbiBitmap->OnClick(_click, _pt, &_rc);
  93. }
  94. else if (_plbiBalloon)
  95. {
  96. hr = _plbiBalloon->OnClick(_click, _pt, &_rc);
  97. }
  98. break;
  99. case DOA_ONMENUSELECT:
  100. if (_plbiButton)
  101. {
  102. hr = _plbiButton->OnMenuSelect(_uId);
  103. }
  104. else if (_plbiBitmapButton)
  105. {
  106. hr = _plbiBitmapButton->OnMenuSelect(_uId);
  107. }
  108. break;
  109. }
  110. _hr = hr;
  111. _Release();
  112. return 0;
  113. }