Publish及Subscribe:使用RProperty进行publish操作
文章信息
- 详细描述
下列代码片段演示了如何利用Symbian IPC发布和订阅机制使用RProperty来发布用户自己的定义的属性。
我们可以使用RProperty::Set()函数来发布属性,这意味着该属性值会发生更新。无论何时属性被发布,所有外部订阅将被完成。
Archived:Using RProperty for subscribing中的代码片段演示了如何订阅用户定义的属性,这个属性定义将在发
布者和订阅者之间通过ExampleProperties.h被共享。
#ifndef EXAMPLEPROPERTIES_H_
#define EXAMPLEPROPERTIES_H_
const TInt KMaxStrLen = 10;
const TUid KExampleProperty = {0xED1917A8};
enum TExamplePropertyKeys { EIntProperty, EStrProperty };
#endif /*EXAMPLEPROPERTIES_H_*/
代码只需自签名即可执行
Contents |
MMP文件
需要下列链接库:
LIBRARY euser.lib
前言
发布或订阅线程都可以通过调用RProperty::Define()来定义属性。用户定义的属性保存在内核中知道操作系统重启或属性被删除。
TInt ret=RProperty::Define(KExampleProperty,EIntProperty,RProperty::EInt);
if (ret != KErrAlreadyExists)
{
User::LeaveIfError(ret);
}
ret= RProperty::Define(KExampleProperty,EStrProperty,RProperty::EByteArray,KMaxStrLen);
if (ret != KErrAlreadyExists)
{
User::LeaveIfError(ret);
}
源文件
#include <e32base.h>
#include <e32property.h>
#include "exampleproperties.h"
LOCAL_C void SetExamplePropertiesL()
{
TInt err(KErrNone);
// Use handle to publish EIntProperty
RProperty intProperty;
err = intProperty.Attach(KExampleProperty, EIntProperty, EOwnerThread);
User::LeaveIfError(err);
err = intProperty.Set(123);
User::LeaveIfError(err);
intProperty.Close();
// Use category and key to publish EStrProperty
err=RProperty::Set(KExampleProperty,EStrProperty,_L("321"));
User::LeaveIfError(err);
}
// Global Functions
GLDEF_C TInt E32Main()
{
// Create cleanup stack
__UHEAP_MARK;
CTrapCleanup* cleanup = CTrapCleanup::New();
// Run application code inside TRAP harness
TRAPD(mainError, SetExamplePropertiesL());
if (mainError)
{
//SetExamplePropertiesL leaves...
}
else
{
//Properties updated ok
}
delete cleanup;
__UHEAP_MARKEND;
return KErrNone;
}
后记
两个示例属性都是用RProperty进行发布。
如果发布者线程已经定义了属性,那也可以通过RProperty::Delete()来删除属性。
TInt err(KErrNone);
err = RProperty::Delete(KExampleProperty,EIntProperty);
if (err != KErrNotFound)
{
User::LeaveIfError(err);
}
err = RProperty::Delete(KExampleProperty,EStrProperty);
if (err != KErrNotFound)
{
User::LeaveIfError(err);
}
当属性删除后,任何订阅者将收到KErrNotFound标识完成。


(no comments yet)