Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6770507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:19:14+00:00 2026-05-26T15:19:14+00:00

I’ve created an event handler that simply returns a list of objects that I

  • 0

I’ve created an event handler that simply returns a list of objects that I receive from a web service when the call completes.

Now I went ahead and ran the app in debug mode and found out that the first time the event is called it works perfectly, but immediately after it completes the event is being fired for a second time. I’ve checked and am absolutely sure I am not calling the event more than once in the receiver class.

This is my first shot at creating custom event handlers inside my applications so I am not entirely sure the implementation is 100% accurate.

Any ideas of what might be causing this? Is the way I created the event handler accurate?

This is the DataHelper class

public class DataHelper
{
    public delegate void DataCalledEventHandler(object sender, List<DataItem> dateItemList);
    public event DataCalledEventHandler DataCalled;

    public DataHelper()
    {

    }

    public void CallData()
    {
        List<DataItem> dataItems = new List<DataItem>();
        //SOME CODE THAT RETURNS DATA
        DataCalled(this, dataItems);
    }
}

This is where I subscribed to my event:

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
   GetNewDataItems();
}
private void GetNewDataItems()
        {

                try
                {
                    DataHelper dataHelper = new DataHelper();
                    dataHelper.CallData();
                    dataHelper.DataCalled += new DataHelper.DataCalledEventHandler(dataHelper_DataCalled);

                }
                catch
                {
                   //Handle any errors
                }
            }
    }

    void dataHelper_DataCalled(object sender, List<DataItem> dataItemsList)
    {
        //Do something with results
        //NOTE: THIS IS WHERE THE EXCEPTION OCCURS WHEN EVENT IS FIRED FOR SECOND TIME
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T15:19:15+00:00Added an answer on May 26, 2026 at 3:19 pm

    Probably you added the delegate twice, is it possible?

    In this case the problem is not in who calls the delegate but in who adds the delegate to the event.

    Probably you did something like…

    private Class1 instance1;
    
    void callback(...)
    {
    }
    
    void myfunction()
    {
        this.instance1.DataCalled += this.callback;
        this.instance1.DataCalled += this.callback;
    }
    

    If not, try to add a breakpoint where you subscribe to the event and see if it is called twice.

    As a side note, you should always check for null when calling an event, if there is no subscriber you can get a NullReferenceException.
    I would also suggest you to use a variable to store the event delegate to avoid the risk of multithreading failure.

    public void CallData()
    {
        List<DataItem> dataItems = new List<DataItem>();
        var handler = this.DataCalled;
        if (handler != null)
            handler(this, dataItems);
    }
    

    Edit: since now I see the code, is obvious that each time you call the GetNewDataItems method you are subsribing every time to the event.
    Do in such a way you subscribe only once, for example, in constructor, or store your variable somewhere or deregister the event when you finish.

    This code contains also a probable memory leak: every time you add a delegate you keep alive both the instance that contains the event and the instance that contains the subscribed method, at least, until both are unreferenced.

    You can try to do something like this…

    void dataHelper_DataCalled(object sender, List<DataItem> dataItemsList)
    {
        // Deregister the event...
        (sender as Class1).DataCalled -= dataHelper_DataCalled; 
    
        //Do something with results
    }
    

    In this way however you must ensure that if there is not an exception during the event registration the event will be fired or you have again memory leaks.

    Instead of an event perhaps you need just a delegate. Of course you should set your delegate field to null when you want to release the delegate.

    // in data helper class
    
    private DataHelper.DataCalledEventHandler myFunctor;
    
    public void CallData(DataHelper.DataCalledEventHandler functor)
    {
        this.myFunctor = functor;
        //SOME CODE THAT RETURNS DATA
    }
    
    // when the call completes, asynchronously...
    private void WhenTheCallCompletes()
    {
        var functor = this.myFunctor;
        if (functor != null)
        {
            this.myFunctor = null;
            List<DataItem> dataItems = new List<DataItem>();
            functor(this, dataItems);
        }
    }
        
    // in your function
    ...    dataHelper.CallData(this.dataHelper_DataCalled);    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am currently running into a problem where an element is coming back from
I have a text area in my form which accepts all possible characters from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.