属性设置器中的线程

发布于 2024-12-12 10:05:58 字数 697 浏览 0 评论 0原文

我有两个属性。第一个是整数属性,这是一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淤浪 2024-12-19 10:05:58

我无法重现你的问题。您使用的是哪个 .NET 框架?

话虽如此,我没有看到在示例代码中使用 BeginInvoke/threading 的好处,因为它将立即等待委托调用完成(使用 EndInvoke)。该属性只会阻塞,并且在功能上与此属性相同:

public int FirstPlaceId
{
    set 
    {
        firstPlaceId = value;
        FirstPlace = setPlaceName(value);
    }
    get { return firstPlaceId; }
}

只需删除 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 (with EndInvoke). That property will just block and is functionally the same this one:

public int FirstPlaceId
{
    set 
    {
        firstPlaceId = value;
        FirstPlace = setPlaceName(value);
    }
    get { return firstPlaceId; }
}

Just remove BeginInvoke.

莫相离 2024-12-19 10:05:58
public void SetFirstPlaceId(int value)
{
    firstPlaceId = value;
    Thread t = new Thread(delegate()
    {
        FirstPlace = setPlaceName(1);
    });
    t.IsBackground = true;
    t.Start();
}

编辑:修复代码以实际启动线程

通过这个修改,它对我来说效果很好。
(我不是 C# 专家,所以我不确定,但我猜您的 delegate 不支持 BeginInvokeEndInvoke代码>出于某种原因。)

public void SetFirstPlaceId(int value)
{
    firstPlaceId = value;
    Thread t = new Thread(delegate()
    {
        FirstPlace = setPlaceName(1);
    });
    t.IsBackground = true;
    t.Start();
}

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 support BeginInvoke and EndInvoke for some reason.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文