Cross thread use of DependencyObject

B

Bill Richards

I run the Unit Test below in debug mode, and I place breakpoints in the
CrossThreadClass on the lines
Owner = p;
and
Dispatcher.Invoke(DispatcherPriority.Normal, new
CrossThreadDelegate(UpdateOwnerAndTokenCount), e.Data);

The Dispatcher line is hit, but the Owner line is never called, even if I
set my thread to sleep for a whole second.

Have I missed something with regards to making cross threaded calls to
DependencyProperty and using the
DependencyObject/Dispatcher/DispatcherObject

Thanks in advance

// -----THE UNIT
TEST--------------------------------------------------------------------------------------------------//

[Test]
public void
WhenAPositionExplodes_ShouldSeeTheNeighbourUnderPlayersControl()
{
PlayerManager manager =
PlayerManager.NewManager(PlayerNumbers.One);
Player player = manager.AddNewPlayer("Player One");

var board = BoardBuilder.CreateBoard(5, 3);
CrossThreadClass corner = board.GetByLocation(new
LocationOnBoard(1, 1));
corner.Owner = player;
corner.AddToken();
corner.AddToken();
corner.AddToken();

CrossThreadClass neighbour = board.GetByLocation(new
LocationOnBoard(2, 1));
System.Threading.Thread.Sleep(20);
Assert.That(neighbour.CurrentTokenCount,
Is.EqualTo(TokenCounts.One));
Assert.That(neighbour.Owner, Is.EqualTo(player));
}

// -----THE
CLASS -----------------------------------------------------------------------------------------------------//
public class CrossThreadClass : DependencyObject
{
public delegate void CrossThreadDelegate(Player p);
public event EventHandler<EventArgs<Player>> OnOwnerChanged;
public event EventHandler<EventArgs<Player>> OnExploding;

public static readonly DependencyProperty OwnerProperty =
DependencyProperty.Register("Owner", typeof(Player)
, typeof(CrossThreadClass)
, new PropertyMetadata(null
, (s, e) =>
RaiseOwnerChangedEventIfRequired((CrossThreadClass)s

, (Player)e.NewValue)));

public Player Owner
{
get { return (Player)GetValue(OwnerProperty); }
set { SetValue(OwnerProperty, value); }
}

private static void
RaiseOwnerChangedEventIfRequired(CrossThreadClass sender, Player newOwner)
{
if (sender.OnOwnerChanged == null)
return;

new Thread(args =>
{
var objects = (object[])args;
var position = (CrossThreadClass)objects[0];
var player = (Player)objects[1];
position.OnOwnerChanged(position, new
EventArgs<Player>(player));
}).Start(new object[] { sender, newOwner });
}

private void UpdateOwnerAndTokenCount(Player p)
{
Owner = p;
AddToken();
}

public void AddToken()
{
// ... stuff to add a token
}

private void NeighbourExplodingEventHandler(object sender,
EventArgs<Player> e)
{
if (CheckAccess())
UpdateOwnerAndTokenCount(e.Data);
else
Dispatcher.Invoke(DispatcherPriority.Normal, new
CrossThreadDelegate(UpdateOwnerAndTokenCount), e.Data);
}

internal void RegisterNeighbourExplodingEvent(ref CrossThreadClass
ctc)
{
ctc.OnExploding += NeighbourExplodingEventHandler;
}
}
 
B

Bill Richards

thanks for the handy hints Peter, and lesson learned :blush:)



Peter Duniho said:

Please do not multi-post. Learn to cross-post correctly, so that there is
only a single message, visible in multiple newsgroups. See this post for
an example of how to do that.

Please _do_ post concise-but-complete code samples. The code you posted
had a great deal of irrelevant code (not concise), while at the same time
could not be compiled as a single file and executed (not complete).

You will find that people are much more willing to take the time to look
at, or even answer, your question if you follow these simple guidelines.

Pete
 
Z

Zhi-Xin Ye [MSFT]

Hello Bill,

Thank you for using Microsoft Managed Newsgroup Service, my name is Zhi-Xin
Ye, it's my pleasure to work with you on this issue.

I'm not able to run the code you posted, could you please send me a sample
code to demonstrate the problem? My email is (e-mail address removed).

I look forward to your reply.

Sincerely,
Zhi-Xin Ye
Microsoft Managed Newsgroup Support Team

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel

free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2

business day is acceptable. Please note that each follow up response may
take approximately 2 business days as the support professional working with
you may

need further investigation to reach the most efficient resolution. The
offering is not appropriate for situations that require urgent, real-time
or

phone-based interactions. Issues of this nature are best handled working
with a dedicated Microsoft Support Engineer by contacting Microsoft
Customer Support

Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Z

Zhi-Xin Ye [MSFT]

Hello Bill,

How about this issue now?Do you still need help on this issue? I you
encounter any difficulty please feel free to let me know.

Best Regards,
Zhi-Xin Ye
Microsoft Managed Newsgroup Support Team

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top