Compartilhar via


XRAutoCriticalSection (Compact 2013)

3/28/2014

This C++ helper class creates a critical section object automatically when you create an instance and deletes it when you release the instance.

Syntax

class XRAutoCriticalSection

Members

The following tables list any constructors and overloaded operators that belong to the class.

Constructors

Constructor

Description

XRAutoCriticalSection.XRAutoCriticalSection()

Initializes the critical section object stored in the XRAutoCriticalSection helper class.

Operators

Overloaded operator

Description

XRAutoCriticalSection.CRITICAL_SECTION&()

Returns the CRITICAL_SECTION object stored in the XRAutoCriticalSection helper class.

XRAutoCriticalSection.operator&

Returns the address of the CRITICAL_SECTION object stored in the XRAutoCriticalSection helper class.

Thread Safety

The members of this class are thread-safe.

Remarks

You can use XRAutoCriticalSection to help ensure thread safety when you are updating data source objects or data source properties.

TPropertyBag<Derived> internally uses the XRAutoCriticalSection class to help ensure thread safety when it registers data source properties.

For more information, see Critical Section Objects on MSDN.

Example

The following example code uses an XRAutoCriticalSection object to help ensure thread safety when it calls TPropertyBag.SetValue(const WCHAR *,XRValue *) to update a data source property.

Important

For readability, the following code example does not contain security checking or error handling. Do not use the following code in a production environment.

MainPage.cpp:
#include "XamlRuntime.h"
#include "XRPropertyBag.h"
#include "oleauto.h"
#include "Data.h"
 
HRESULT MainPage::TestCriticalSection()
{
// Define variables for the data source object and property.
XRPtr<ClassName> pObj;
XRValue xrvalueCurrent;
XRValue xrvalueNew;
TBoundProperty<BSTR> bstrProp;

// Set a BSTR string for the new XRValue object.
bstrProp = SysAllocString(L"UpdatedStringValue");
xrvalueNew.bstrStringVal = bstrProp;

// Define a critical section object.
XRAutoCriticalSection csObject;

// Create an instance of a data source object and initialize its properties.
ClassName::CreateInstance(&pObj);
pObj->InitializeProperties();

// Request mutually exclusive access to the thread.
EnterCriticalSection(&csObject);
pObj->GetValue(L"PropName1", &xrvalueCurrent);

// Update a data source property.
if (xrvalueCurrent.bstrStringVal != bstrProp)
{
     pObj->SetValue(L"PropName1", &xrvalueNew);
}
// Release the critical section object.
LeaveCriticalSection(&csObject);

return S_OK;
}

Data.h:
class _declspec(uuid("{0ED08C01-200A-42ed-BB7C-A4ED016B65B7}"))
ClassName : public TPropertyBag<ClassName>
{
protected:
ClassName() {};

public:
TBoundProperty<BSTR> bstrProp1;
TBoundProperty<BSTR> bstrProp2;

HRESULT InitializeProperties()
{
     HRESULT hr = S_OK;
     hr = BeginRegisterProperties();
     if (FAILED(hr))
     {
          return hr;
     }
     hr = RegisterBoundProperty(L"PropName1", bstrProp1);
     hr = RegisterBoundProperty(L"PropName2", bstrProp2);
     hr = EndRegisterProperties();
     return hr;
}
};

Requirements

Header

winbase.h,
XRPropertyBag.h

See Also

Reference

Classes for Populating UI Elements with Data

Other Resources

EnterCriticalSection
LeaveCriticalSection