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 6561451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:35:19+00:00 2026-05-25T13:35:19+00:00

I have a problem with some code I need to refactor. Right now it

  • 0

I have a problem with some code I need to refactor. Right now it uses lambdas as event handlers, but they are not removed properly. From what I have read, this is not even possible? Anyway I would like to rewrite it to use a delegate instead of an anonymous function, and now my problem is that right now it takes an action as parameter, and I can’t seem to figure out how to pass the action on to my new delegate. This is the code:

void RetrieveData(
            int pointId,
            int? chartCollectionId,
            Action action)
        {
            if (pointId <= 0)
                throw new ArgumentException("PointId not valid");

            LastPointId = NextPointId;
            NextPointId = pointId;

            Clear();

            _csr = new CustomerServiceRepository();

            _csr.ServiceClient.GetChartDataCompleted += (se, ea) =>
                                                            {
                                                                _cachedCharts = ea.Result;
                                                                ChartDataRetrieved(ea.Result);
                                                                if (action != null)
                                                                    action.Invoke();
                                                                _csr = null;
                                                            };
            _csr.ServiceClient.GetChartDataAsync(
                Settings.Current.Customer.CustomerName,
                pointId,
                chartCollectionId);

            _csr.ServiceClient.GetChartDataCompleted -= (se, ea) => //remove after usage
                                                            {
                                                                _cachedCharts = ea.Result;
                                                                ChartDataRetrieved(ea.Result);
                                                                if (action != null)
                                                                    action.Invoke();
                                                                _csr = null;
                                                            };
        }

I was thinking that maybe I could create the following:

public class extendedEventArgs : GetChartDataCompletedEventArgs
        {
            Action foo { get; set; }
        }

        void tang(object sender, extendedEventArgs e)
        {
            _cachedCharts = e.Result;
            ChartDataRetrieved(e.Result);
            if (action != null)
                action.Invoke();
            _csr = null;
        }

And the pass the action as a parameter in the extended event args, but when I try to use it like this

_csr.ServiceClient.GetChartDataCompleted += new EventHandler<extendedEventHandler>(tang);

It gives an error:

Cannot implicitly convert type  System.EventHandler<Conwx.Net.Client.CustomerClient.Controls.ChartControls.ChartListForecast.extendedEventArgs>'  to  System.EventHandler<Conwx.Net.Client.Framework.CustomerServiceReference.GetChartDataCompletedEventArgs>'

What am I doing wrong here? Alternative solutions are also welcome.

.K

  • 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-25T13:35:20+00:00Added an answer on May 25, 2026 at 1:35 pm

    As I read it, the key problem here is not being able to remove the handler; if so, all you need it to store the delegate (where in the below, YourDelegateType is meant to mean: the defined type of GetChartDataCompleted):

    YourDelegateType handler = (se, ea) =>
        {
            _cachedCharts = ea.Result;
            ChartDataRetrieved(ea.Result);
            if (action != null)
                action.Invoke();
            _csr = null;
        };
    _csr.ServiceClient.GetChartDataCompleted += handler;
    ...
    _csr.ServiceClient.GetChartDataCompleted -= handler;
    

    You can also make it self-unsubscribing (i.e. so that it unsubscribes when the event is raised):

    YourDelegateType handler = null;
    handler = (se, ea) =>
        {
            _cachedCharts = ea.Result;
            ChartDataRetrieved(ea.Result);
            if (action != null)
                action.Invoke();
            _csr.ServiceClient.GetChartDataCompleted -= handler;
            _csr = null;
        };
    _csr.ServiceClient.GetChartDataCompleted += handler;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an intermittent problem with some code that writes to a Windows Event
I need to abstract some behavioural code and have a problem trying to reference
I have some problem convert C++ code to PHP. The purpose is I need
I have a problem with some code and I believe it is because of
Here's my problem - I have some code like this: <mx:Canvas width=300 height=300> <mx:Button
I have some problem with my code public IQueryable<PageItems> GetPageById(Guid Id) { var xml
I have some problem with my cfml website. I have used the below code
I have run into a bit of a tricky problem in some C++ code,
i am new in JSP,i have some problem with the following code : <%@
I have some code that I'd like to run on a page. My problem

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.