Skip to content

Commit

Permalink
Add Agile flag (#2344)
Browse files Browse the repository at this point in the history
* Add Agile flag

* Add comments about REGCLS_AGILE
  • Loading branch information
pmpurifoy authored Apr 1, 2022
1 parent 003df95 commit 4b9e16e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
9 changes: 7 additions & 2 deletions dev/AppNotifications/AppNotificationManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,17 @@ namespace winrt::Microsoft::Windows::AppNotifications::implementation
THROW_HR_IF_MSG(E_INVALIDARG, m_notificationComActivatorRegistration, "Already Registered for App Notifications!");

// Check if the caller has registered event handlers, if so the REGCLS_MULTIPLEUSE flag will cause COM to ensure that all activators
// are routed inproc, otherwise with REGCLS_MULTIPLEUSE COM will launch a new process of the Win32 app for each invocation.
// are routed inproc, otherwise with REGCLS_SINGLEUSE COM will launch a new process of the Win32 app for each invocation.
auto activationFlag{ m_notificationHandlers ? REGCLS_MULTIPLEUSE : REGCLS_SINGLEUSE };

// Register an INotificationActivationCallback to receive background activations from AppNotification.
// Also, STA threads that call CoRegisterClassObject need to use the REGCLS_AGILE flag so that the object is
// associated with the neutral apartment. This allows other threads to activate the STA registered thread.
THROW_IF_FAILED(::CoRegisterClassObject(
AppModel::Identity::IsPackagedProcess() ? registeredClsid : storedComActivatorGuid,
winrt::make<AppNotificationManagerFactory>().get(),
CLSCTX_LOCAL_SERVER,
m_notificationHandlers ? REGCLS_MULTIPLEUSE : REGCLS_SINGLEUSE,
activationFlag | REGCLS_AGILE,
&m_notificationComActivatorRegistration));
}
}
Expand Down
22 changes: 18 additions & 4 deletions dev/PushNotifications/PushNotificationManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -467,13 +467,20 @@ namespace winrt::Microsoft::Windows::PushNotifications::implementation

{
auto lock{ m_lock.lock_exclusive() };
// Register a PushNotificationBackgroundTask to handle background activation scenarios
m_waitHandleForArgs.create();

// Check if the caller has registered event handlers, if so the REGCLS_MULTIPLEUSE flag will cause COM to ensure that all activators
// are routed inproc, otherwise with REGCLS_SINGLEUSE COM will launch a new process of the Win32 app for each invocation.
auto activationFlags{ m_foregroundHandlers ? REGCLS_MULTIPLEUSE : REGCLS_SINGLEUSE };

// Register a PushNotificationBackgroundTask to handle background activation.
// STA threads that call CoRegisterClassObject need to use the REGCLS_AGILE flag so that the object is
// associated with the neutral apartment. This allows other threads to activate the STA registered thread.
THROW_IF_FAILED(::CoRegisterClassObject(
m_registeredClsid,
winrt::make<PushNotificationManagerFactory>().get(),
CLSCTX_LOCAL_SERVER,
m_foregroundHandlers ? REGCLS_MULTIPLEUSE : REGCLS_SINGLEUSE,
activationFlags | REGCLS_AGILE,
&m_comActivatorRegistration));
}

Expand Down Expand Up @@ -550,13 +557,20 @@ namespace winrt::Microsoft::Windows::PushNotifications::implementation
if (AppModel::Identity::IsPackagedProcess())
{
auto lock{ m_lock.lock_exclusive() };

m_waitHandleForArgs.create();

// Check if the caller has registered event handlers, if so the REGCLS_MULTIPLEUSE flag will cause COM to ensure that all activators
// are routed inproc, otherwise with REGCLS_SINGLEUSE COM will launch a new process of the Win32 app for each invocation.
auto activationFlags{ m_foregroundHandlers ? REGCLS_MULTIPLEUSE : REGCLS_SINGLEUSE };

// Register a PushNotificationBackgroundTask to handle background activation.
// Also, STA threads that call CoRegisterClassObject need to use the REGCLS_AGILE flag so that the object is
// associated with the neutral apartment. This allows other threads to activate the STA registered thread.
THROW_IF_FAILED(::CoRegisterClassObject(
m_registeredClsid,
winrt::make<PushNotificationManagerFactory>().get(),
CLSCTX_LOCAL_SERVER,
m_foregroundHandlers ? REGCLS_MULTIPLEUSE : REGCLS_SINGLEUSE,
activationFlags | REGCLS_AGILE,
&m_comActivatorRegistration));
}

Expand Down

0 comments on commit 4b9e16e

Please sign in to comment.