Use wxWinAPI::Event wrapper class instead of raw Windows event handles.

This makes the code slightly shorter and, more importantly, more readable and
safer.

Closes #16233.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@76655 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2014-06-02 01:15:30 +00:00
parent 70bcb9ac46
commit 0ae3d1d22a
2 changed files with 21 additions and 37 deletions

View File

@@ -40,6 +40,7 @@
#include "wx/msw/private.h" #include "wx/msw/private.h"
#include "wx/msw/private/hiddenwin.h" #include "wx/msw/private/hiddenwin.h"
#include "wx/msw/private/event.h"
#include "wx/dynlib.h" #include "wx/dynlib.h"
wxDEFINE_EVENT( wxEVT_DIALUP_CONNECTED, wxDialUpEvent ); wxDEFINE_EVENT( wxEVT_DIALUP_CONNECTED, wxDialUpEvent );
@@ -137,8 +138,6 @@ struct WXDLLEXPORT wxRasThreadData
wxRasThreadData() wxRasThreadData()
{ {
hWnd = 0; hWnd = 0;
hEventRas =
hEventQuit = 0;
dialUpManager = NULL; dialUpManager = NULL;
} }
@@ -146,17 +145,11 @@ struct WXDLLEXPORT wxRasThreadData
{ {
if ( hWnd ) if ( hWnd )
DestroyWindow(hWnd); DestroyWindow(hWnd);
if ( hEventQuit )
CloseHandle(hEventQuit);
if ( hEventRas )
CloseHandle(hEventRas);
} }
HWND hWnd; // window to send notifications to HWND hWnd; // window to send notifications to
HANDLE hEventRas, // automatic event which RAS signals when status changes wxWinAPI::Event hEventRas, // automatic event which RAS signals when status changes
hEventQuit; // manual event which we signal when we terminate hEventQuit; // manual event which we signal when we terminate
class WXDLLIMPEXP_FWD_CORE wxDialUpManagerMSW *dialUpManager; // the owner class WXDLLIMPEXP_FWD_CORE wxDialUpManagerMSW *dialUpManager; // the owner
}; };
@@ -575,12 +568,10 @@ void wxDialUpManagerMSW::CleanUpThreadData()
{ {
if ( m_hThread ) if ( m_hThread )
{ {
if ( !SetEvent(m_data->hEventQuit) ) if ( m_data->hEventQuit.Set() )
{
wxLogLastError(wxT("SetEvent(RasThreadQuit)"));
}
else // sent quit request to the background thread
{ {
// sent quit request to the background thread
// the thread still needs m_data so we can't free it here, rather // the thread still needs m_data so we can't free it here, rather
// let the thread do it itself // let the thread do it itself
m_data = NULL; m_data = NULL;
@@ -1066,14 +1057,11 @@ bool wxDialUpManagerMSW::EnableAutoCheckOnlineStatus(size_t nSeconds)
if ( ok ) if ( ok )
{ {
// first create an event to wait on // first create an event to wait on
m_data->hEventRas = ::CreateEvent if ( !m_data->hEventRas.Create
( (
NULL, // security attribute (default) wxWinAPI::Event::AutomaticReset,
FALSE, // manual reset (no, it is automatic) wxWinAPI::Event::Nonsignaled
FALSE, // initial state (not signaled) ) )
NULL // name (no)
);
if ( !m_data->hEventRas )
{ {
wxLogLastError(wxT("CreateEvent(RasStatus)")); wxLogLastError(wxT("CreateEvent(RasStatus)"));
@@ -1087,14 +1075,11 @@ bool wxDialUpManagerMSW::EnableAutoCheckOnlineStatus(size_t nSeconds)
// here avoids problems with missing the event if wxDialUpManagerMSW // here avoids problems with missing the event if wxDialUpManagerMSW
// is created and destroyed immediately, before wxRasStatusWindowProc // is created and destroyed immediately, before wxRasStatusWindowProc
// starts waiting on the event // starts waiting on the event
m_data->hEventQuit = ::CreateEvent if ( !m_data->hEventQuit.Create
( (
NULL, // default security wxWinAPI::Event::ManualReset,
TRUE, // manual event wxWinAPI::Event::Nonsignaled
FALSE, // initially non signalled ) )
NULL // nameless
);
if ( !m_data->hEventQuit )
{ {
wxLogLastError(wxT("CreateEvent(RasThreadQuit)")); wxLogLastError(wxT("CreateEvent(RasThreadQuit)"));

View File

@@ -75,6 +75,7 @@
#endif // wxUSE_IPC #endif // wxUSE_IPC
#include "wx/msw/private/hiddenwin.h" #include "wx/msw/private/hiddenwin.h"
#include "wx/msw/private/event.h"
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// constants // constants
@@ -94,7 +95,7 @@ static const wxChar *wxMSWEXEC_WNDCLASSNAME = wxT("_wxExecute_Internal_Class");
static const wxChar *gs_classForHiddenWindow = NULL; static const wxChar *gs_classForHiddenWindow = NULL;
// event used to wake up threads waiting in wxExecuteThread // event used to wake up threads waiting in wxExecuteThread
static HANDLE gs_heventShutdown = NULL; static wxWinAPI::Event gs_heventShutdown;
// handles of all threads monitoring the execution of asynchronously running // handles of all threads monitoring the execution of asynchronously running
// processes // processes
@@ -139,17 +140,16 @@ public:
virtual bool OnInit() { return true; } virtual bool OnInit() { return true; }
virtual void OnExit() virtual void OnExit()
{ {
if ( gs_heventShutdown ) if ( gs_heventShutdown.IsOk() )
{ {
// stop any threads waiting for the termination of asynchronously // stop any threads waiting for the termination of asynchronously
// running processes // running processes
if ( !::SetEvent(gs_heventShutdown) ) if ( !gs_heventShutdown.Set() )
{ {
wxLogDebug(wxT("Failed to set shutdown event in wxExecuteModule")); wxLogDebug(wxT("Failed to set shutdown event in wxExecuteModule"));
} }
::CloseHandle(gs_heventShutdown); gs_heventShutdown.Close();
gs_heventShutdown = NULL;
// now wait until they terminate // now wait until they terminate
if ( !gs_asyncThreads.empty() ) if ( !gs_asyncThreads.empty() )
@@ -287,11 +287,10 @@ static DWORD __stdcall wxExecuteThread(void *arg)
wxExecuteData * const data = (wxExecuteData *)arg; wxExecuteData * const data = (wxExecuteData *)arg;
// create the shutdown event if we're the first thread starting to wait // create the shutdown event if we're the first thread starting to wait
if ( !gs_heventShutdown ) if ( !gs_heventShutdown.IsOk() )
{ {
// create a manual initially non-signalled event object // create a manual initially non-signalled event object
gs_heventShutdown = ::CreateEvent(NULL, TRUE, FALSE, NULL); if ( !gs_heventShutdown.Create(wxWinAPI::Event::ManualReset, wxWinAPI::Event::Nonsignaled) )
if ( !gs_heventShutdown )
{ {
wxLogDebug(wxT("CreateEvent() in wxExecuteThread failed")); wxLogDebug(wxT("CreateEvent() in wxExecuteThread failed"));
} }