属性设置器中的线程
我有两个属性。第一个是整数属性,这是一个 ID。第二个是一个String,它对应于ID。 当我设置ID时,我会在单独的线程中设置相应的字符串属性。 代码片段是:
public int FirstPlaceId
{
set
{
firstPlaceId = value;
setPlaceNameDelegate d = new setPlaceNameDelegate( setPlaceName );
IAsyncResult iar = d.BeginInvoke( value, null, null );
FirstPlace = d.EndInvoke( iar );
}
get { return firstPlaceId; }
}
public string FirstPlace { set; get; }
private string setPlaceName(int id)
{
return "alma";
}
delegate String setPlaceNameDelegate( int id );
methodus()
{
FirstPlaceId = 5;
}
我认为看起来不错。但在运行时,当我向 FirstPlaceId 属性分配某个值 (5) 时,会发生 NotSupportedException 错误。 为什么以及解决方案是什么? 谢谢
I have two properties. The first one is an integer property, this is an ID. The second one is a String, it's corresponding to the ID.
When I set the ID, I will set the correspondent string property in a separate thread.
The code snipet is:
public int FirstPlaceId
{
set
{
firstPlaceId = value;
setPlaceNameDelegate d = new setPlaceNameDelegate( setPlaceName );
IAsyncResult iar = d.BeginInvoke( value, null, null );
FirstPlace = d.EndInvoke( iar );
}
get { return firstPlaceId; }
}
public string FirstPlace { set; get; }
private string setPlaceName(int id)
{
return "alma";
}
delegate String setPlaceNameDelegate( int id );
methodus()
{
FirstPlaceId = 5;
}
I think it looks like ok. But in runtime when I assign some value (5) to the FirstPlaceId property, the NotSupportedException error occurs.
Why and what is the solution?
Thx
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我无法重现你的问题。您使用的是哪个 .NET 框架?
话虽如此,我没有看到在示例代码中使用
BeginInvoke
/threading 的好处,因为它将立即等待委托调用完成(使用EndInvoke
)。该属性只会阻塞,并且在功能上与此属性相同:只需删除
BeginInvoke
。I can't reproduce your issue. Which .NET Framework are you using?
With that said, I don't see the benefit of using
BeginInvoke
/threading in your sample code since it will immediately wait for the delegate call to complete (withEndInvoke
). That property will just block and is functionally the same this one:Just remove
BeginInvoke
.编辑:修复代码以实际启动
线程
。通过这个修改,它对我来说效果很好。
(我不是 C# 专家,所以我不确定,但我猜您的
delegate
不支持BeginInvoke
和EndInvoke
代码>出于某种原因。)EDIT: code fixed to actually start the
Thread
.With this modification it just works fine for me.
(And I'm not a C# expert so I don't know for sure, but I guess that your
delegate
doesn't supportBeginInvoke
andEndInvoke
for some reason.)