Reputation: 649
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
Upvotes: 0
Views: 356
Reputation: 1857
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.)
Upvotes: 0
Reputation: 5355
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
.
Upvotes: 1