MainProcess.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include "StdAfx.h"
  2. #include "MainProcess.h"
  3. #include "WinService.h"
  4. CMainProcess::CMainProcess(void)
  5. {
  6. m_hBackupDatabase = NULL;
  7. m_hCheckServices = NULL;
  8. m_hEvent1 = NULL;
  9. m_hEvent2 = NULL;
  10. }
  11. CMainProcess::~CMainProcess(void)
  12. {
  13. }
  14. void CMainProcess::StartWork()
  15. {
  16. m_hEvent1 = CreateEvent(NULL, TRUE, FALSE, NULL);
  17. if ( m_hEvent1 == NULL )
  18. {
  19. //_tprintf_s(_T("创建事件失败\n"));
  20. return;
  21. }
  22. m_hEvent2 = CreateEvent(NULL, TRUE, FALSE, NULL);
  23. if ( m_hEvent2 == NULL )
  24. {
  25. //_tprintf_s(_T("创建事件失败2\n"));
  26. return;
  27. }
  28. m_hCheckServices = CreateThread(NULL, 0, CheckSvcStatusThread, this, 0, NULL);
  29. if ( m_hCheckServices == NULL)
  30. {
  31. SetEvent(m_hEvent1);
  32. SetEvent(m_hEvent2);
  33. CloseHandle(m_hEvent1);
  34. CloseHandle(m_hEvent2);
  35. m_hEvent1 = NULL;
  36. m_hEvent2 = NULL;
  37. //_tprintf_s(_T("创建线程失败\n"));
  38. return;
  39. }
  40. m_hBackupDatabase = CreateThread(NULL, 0, BackupDatabaseThread, this, 0, NULL);
  41. if ( m_hBackupDatabase == NULL )
  42. {
  43. SetEvent(m_hEvent1);
  44. SetEvent(m_hEvent2);
  45. CloseHandle(m_hEvent1);
  46. CloseHandle(m_hEvent2);
  47. m_hEvent1 = NULL;
  48. m_hEvent2 = NULL;
  49. WaitForSingleObject(m_hCheckServices,INFINITE);
  50. if (m_hBackupDatabase)
  51. CloseHandle(m_hBackupDatabase);
  52. m_hBackupDatabase = NULL;
  53. //_tprintf_s(_T("创建线程失败2\n"));
  54. return;
  55. }
  56. }
  57. void CMainProcess::EndofWork()
  58. {
  59. if ( m_hEvent1 )
  60. {
  61. SetEvent(m_hEvent1);
  62. CloseHandle(m_hEvent1);
  63. }
  64. if ( m_hEvent2 )
  65. {
  66. SetEvent(m_hEvent2);
  67. CloseHandle(m_hEvent2);
  68. }
  69. if ( m_hCheckServices )
  70. {
  71. WaitForSingleObject(m_hCheckServices,INFINITE);
  72. CloseHandle(m_hCheckServices);
  73. }
  74. if ( m_hBackupDatabase )
  75. {
  76. WaitForSingleObject(m_hBackupDatabase,INFINITE);
  77. CloseHandle(m_hBackupDatabase);
  78. }
  79. }
  80. DWORD CMainProcess::CheckSvcStatusThread(LPVOID lpVoid)
  81. {
  82. CMainProcess *pthis = (CMainProcess*)lpVoid;
  83. do
  84. {
  85. } while ( WaitForSingleObject(pthis->m_hEvent1, 200) == WAIT_TIMEOUT );
  86. return 0;
  87. }
  88. DWORD CMainProcess::BackupDatabaseThread(LPVOID lpVoid)
  89. {
  90. CMainProcess *pthis = (CMainProcess*)lpVoid;
  91. do
  92. {
  93. } while ( WaitForSingleObject(pthis->m_hEvent2, 200) == WAIT_TIMEOUT );
  94. return 0;
  95. }