Event.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * Copyright: JessMA Open Source (ldcsaa@gmail.com)
  3. *
  4. * Version : 2.3.15
  5. * Author : Bruce Liang
  6. * Website : http://www.jessma.org
  7. * Project : https://github.com/ldcsaa
  8. * Blog : http://www.cnblogs.com/ldcsaa
  9. * Wiki : http://www.oschina.net/p/hp-socket
  10. * QQ Group : 75375912
  11. *
  12. * Licensed under the Apache License, Version 2.0 (the "License");
  13. * you may not use this file except in compliance with the License.
  14. * You may obtain a copy of the License at
  15. *
  16. * http://www.apache.org/licenses/LICENSE-2.0
  17. *
  18. * Unless required by applicable law or agreed to in writing, software
  19. * distributed under the License is distributed on an "AS IS" BASIS,
  20. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  21. * See the License for the specific language governing permissions and
  22. * limitations under the License.
  23. */
  24. #pragma once
  25. class CEvt
  26. {
  27. public:
  28. CEvt(BOOL bManualReset = FALSE, BOOL bInitialState = FALSE, LPCTSTR pszName = nullptr, LPSECURITY_ATTRIBUTES pSecurity = nullptr)
  29. {
  30. m_hEvent = ::CreateEvent(pSecurity, bManualReset, bInitialState, pszName);
  31. ASSERT(IsValid());
  32. }
  33. ~CEvt()
  34. {
  35. if(IsValid())
  36. VERIFY(::CloseHandle(m_hEvent));
  37. }
  38. BOOL Open(DWORD dwAccess, BOOL bInheritHandle, LPCTSTR pszName)
  39. {
  40. if(IsValid())
  41. VERIFY(::CloseHandle(m_hEvent));
  42. m_hEvent = ::OpenEvent(dwAccess, bInheritHandle, pszName);
  43. return(IsValid());
  44. }
  45. BOOL Pulse() {return(::PulseEvent(m_hEvent));}
  46. BOOL Reset() {return(::ResetEvent(m_hEvent));}
  47. BOOL Set() {return(::SetEvent(m_hEvent));}
  48. HANDLE& GetHandle () {return m_hEvent;}
  49. operator HANDLE () {return m_hEvent;}
  50. BOOL IsValid () {return m_hEvent != nullptr;}
  51. private:
  52. CEvt(const CEvt&);
  53. CEvt operator = (const CEvt&);
  54. private:
  55. HANDLE m_hEvent;
  56. };