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

View File

@@ -75,6 +75,7 @@
#endif // wxUSE_IPC
#include "wx/msw/private/hiddenwin.h"
#include "wx/msw/private/event.h"
// ----------------------------------------------------------------------------
// constants
@@ -94,7 +95,7 @@ static const wxChar *wxMSWEXEC_WNDCLASSNAME = wxT("_wxExecute_Internal_Class");
static const wxChar *gs_classForHiddenWindow = NULL;
// 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
// processes
@@ -139,17 +140,16 @@ public:
virtual bool OnInit() { return true; }
virtual void OnExit()
{
if ( gs_heventShutdown )
if ( gs_heventShutdown.IsOk() )
{
// stop any threads waiting for the termination of asynchronously
// running processes
if ( !::SetEvent(gs_heventShutdown) )
if ( !gs_heventShutdown.Set() )
{
wxLogDebug(wxT("Failed to set shutdown event in wxExecuteModule"));
}
::CloseHandle(gs_heventShutdown);
gs_heventShutdown = NULL;
gs_heventShutdown.Close();
// now wait until they terminate
if ( !gs_asyncThreads.empty() )
@@ -287,11 +287,10 @@ static DWORD __stdcall wxExecuteThread(void *arg)
wxExecuteData * const data = (wxExecuteData *)arg;
// 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
gs_heventShutdown = ::CreateEvent(NULL, TRUE, FALSE, NULL);
if ( !gs_heventShutdown )
if ( !gs_heventShutdown.Create(wxWinAPI::Event::ManualReset, wxWinAPI::Event::Nonsignaled) )
{
wxLogDebug(wxT("CreateEvent() in wxExecuteThread failed"));
}