none
Strange code execution without prior call to it

    General discussion

  • I have attached a picture where I have 2 breakpoints: first one is never executed! How is it possible then that second one is. And I will post some code to explain the structure of the model:

    base abstract class (showing only information of importance)

    protected internal virtual void RefereshList()
    {
        GetItems();
    }
    
    protected abstract void GetItems();

    I have like 10 classes (ClassA, ClassB...) that inherits from this class (BaseClass). An implementation  of GetItems from ClassA:

    protected override void GetItems()
    {
    
        // THIS CODE NEVER GETS EXECUTED
        IsBusy = true;
                
        // load data asynchronously
        _model.GetUnitsAsync(a =>
        {
    
            // THIS CODE GETS EXECUTED
            if (a.HasException)
            {
                // Notify UI about the exception during data retrieval
                ErrorMessage = a.Exception;
            }
            else
            {
                // get the list
                List = new ObservableCollection<UnitOfMeasurement>(a.Results);
            }
    
            IsBusy = false;
        });
    }

    The strangest thing here is that I am GetItems is never called, and therefore should never be executed, so how is that anonymous method being executed? And what is even more strange, the behavior is totally random. For example:

    1) Start the application, open a screen that uses ClassA, only a ClassA.GetItems is being executed, so both breakpoints are active - EXPECTED BEHAVIOR

    2) Start the application, open a screen that uses ClassA, only a screen that uses ClassA.GetItems is being executed, so both breakpoints are active - EXPECTED BEHAVIOR

    ....... after several openings/closings

    3) Start the application, open a screen that uses ClassA, a screen that uses ClassA.GetItems is being executed, so both breakpoints are active - HOWEVER, addional ClassB.GetItems method is executed, which was never called - UNEXPECTED BEHAVIOR

    4) Start the application, open a screen that uses ClassA, a screen that uses ClassA.GetItems is being executed, so both breakpoints are active - HOWEVER, several addional GetItems (ClassB.GetItems, ClassE.GetItems...) are being executed, which were never called - UNEXPECTED BEHAVIOR

    5) Start the application, open a screen that uses ClassA, only a screen that uses ClassA.GetItems is being executed, so both breakpoints are active - EXPECTED BEHAVIOR

    BaseClass, ClassA, ClassB, etc are in the separate project (ViewModel), _model (defined in separate project - Model) is injected into ViewModels via SimpleIoc (MVVM Light).

    Why is this happening? Each call is making an unecessery trip to server via RIA services.

    http://pictat.com/show.php?i=/2012/4/13/22132untitled.gif

    Wednesday, April 11, 2012 8:54 AM

All replies

  • Hi Goran,

    I cannot reproduce this in a simple project.

    Could you upload a runnable project onto SkyDrive then we can test it on our side.

     

    Best Regards,

    Friday, April 13, 2012 2:13 AM
  • Hi Shi,

    Is it a public resource that anyone can access, or MS will only have access to it? Because this is a 40% stage project, the client will not allow anyone to have access to it.

    Regards,

    Goran

    Friday, April 13, 2012 8:50 AM
  • Try changing the name from GetItems to something else.  Maybe there is a different ancestor whose GetItems is being overridden?

    Friday, April 13, 2012 9:54 AM
  • Try changing the name from GetItems to something else.  Maybe there is a different ancestor whose GetItems is being overridden?

    If I change, I would get two errors: one that there is no GetTimes2 to override, and another that I am not implementing GetItems. I have checked whole solution for name colisions, and nothing.

    Even if there were naming colisions, if you look at the image I provided there is NO WAY that anonymous method should be executed if the first breakpoint didnt happend. Also, _model.GetUnitsAsync exists only on one place in the solution, but even if existed on several, it would execute its own anonymous method, right?

    Today I didnt receive not a single problem, everything works ok. It seems that this problem occurs only when particular pieces come together, but I dont know how. Somedays it doesnt happend at all, but when it happens, then it is quite frequent. When it starts hapenning again, I am going to take a video. :)

    Anyway, if you look at the picture, what circumstances can cause that 2nd breakpoint is fired, while 1st isnt? This is what was hapenning to me. Also, if it was a problem in the code, should the problem appear regularly, and not without any order - sometimes it does, sometimes it doesnt.

    Friday, April 13, 2012 12:05 PM
  • You have to change in the ancestor before you can override it in the descendents.

    What does the call stack look like when it DOES hit the routine unexpectedly?

    Friday, April 13, 2012 4:26 PM
  • You have to change in the ancestor before you can override it in the descendents.

    What does the call stack look like when it DOES hit the routine unexpectedly?

    So, your idea was to change on both places? What am I testing with this approach? Since there is only one base class and lots of descendants that inherit only from this base, then there is no two implementations for sure (in base class this method is declared as abstract). Complete ViewModel is in the separate project. so currentlly there is like only 10 classes in it - easy to search. Referenced projects do not have anywhare a namespace that ViewModel has, so this is excluded. In the View project, which references the ViewModel project, I have checked that each ViewModel is created only in its View, so no cross-reference is there. The only thing I didnt check was if the View itself is not somehow created, but not shown (to check this, now I placed breakpoints in contructor for all Views). Still, if this was the case, it would hit both breakpoints, so I would know something is wrong with the View code.

    Good suggestion with the stack trace, there could be some meaningfull information there. As soon as I catch this behavior, I will check the stack trace and post it here. Today it happened only once, contrary to the day when I posted the question, when it was happening almost every time I started the app (and I didnt change anything in the ViewModel project).

    Friday, April 13, 2012 7:36 PM
  • Oee.Client.ViewModel!Oee.Client.ViewModel.MachineViewModel.GetItems.AnonymousMethod__0(Oee.Client.Common.EntityResultArgs<Oee.Server.Data.Machine> a) Line 52    C#
    Oee.Client.Model!Oee.Client.Model.OeeModel.PerformQuery<Oee.Server.Data.Machine>.AnonymousMethod__e(System.ServiceModel.DomainServices.Client.LoadOperation<Oee.Server.Data.Machine> c) Line 564 + 0x7c bytes    C#
    [External Code]    
    

    This is what was reported by the call stack. It is not clear what has instantiated the first call.

    Saturday, April 14, 2012 7:57 PM
  • Try NOT using an anonymous method.  And in the event handler, remove the previous one.  That way there should only ever be one.

    Tuesday, April 24, 2012 9:02 AM