Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am creating a small game in UWP using C# and a physics library.

After some rather painful debugging, I have discovered that some code has been run behind the scene after a _Click event has been executed. This code needs to fire before my event code has completed.

In other words, this is what I want to happen:

1. user clicks button
2. some code executes via the _Click event
3. the final parts of this code fetches some attributes from a list, regarding the object just added to the list.

What happens now is this:
1. user clicks button
2. some code executes via the _Click event
3. the final part of the code tries to fetch some attributes, but as these haven't been modified yet in the underlying code, my code crashes.

I have no problem waiting a few milliseconds or so before fetching the updated object, so I can easily cut out the final code and place it in a method that runs a little later in time (I'd prefer this to digging into the underlying code library). Thus, I wonder if there is a something similar to a Page_Loaded event, taking place AFTER the underlying code has been updated correctly.

Here is my _Click code. Nothing strange here really. The problem is that the underlying code runs AFTER the closing of this method. I need a way to run more code AFTER that underlying code has finished running.
C#
private void btnAddBomber_Click(object sender, RoutedEventArgs e)
{
    ucBomber bomber = new ucBomber();
    AddObjectRandomPosition(bomber, 50, 1000, 120, 120, 174, 100);
}


What I have tried:

I have tried to find a suitable event, but to no avail. I am not sure of how to search for this.
Posted
Updated 18-Jun-16 4:09am
v2

1 solution

You can't run code "after" the event handler finishes, and there isn't anything which occurs automatically once the event handler is finished - unless you have added extra handlers to the same event, in which case they will be executed in turn. So check the event and make sure that you haven't added a second or third handler to the event list.

To prove what the problem is, comment out the click handler content - and I'm pretty sure that your problem will disappear. If it doesn't, then it's a coincidence that this happens when you click the button and you need to look at the rest of your code.
If it does disappear, then start by looking at the two methods you call from the Click event handler: the ucBomber constructor, and the AddObjectRandomPosition method.
The chances are the it's the second one, and what it may be doing is causing a re-paint via the adding in of the object and some part of the paint handler is causing your problem.
 
Share this answer
 
Comments
petter2012 18-Jun-16 11:46am    
Thanks a lot for your reply. The fault is not in the two methods (the list hasn't been updated there yet) but I'll look to you other suggestions. Thanks again.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900