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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:32:26+00:00 2026-06-13T02:32:26+00:00

I am getting this warning (Implicity captured closure: this) from Resharper: does this mean

  • 0

I am getting this warning (“Implicity captured closure: this”) from Resharper: does this mean that somehow this code is capturing the entire enclosing object?

    internal Timer Timeout = new Timer
                            {
                                Enabled = false,
                                AutoReset = false
                            };
    public Task<Response> ResponseTask
    {
        get
        {
            var tcs = new TaskCompletionSource<Response>();

            Timeout.Elapsed += (e, a) => tcs.SetException(new TimeoutException("Timeout at " + a.SignalTime));

            if (_response != null) tcs.SetResult(_response);
            else ResponseHandler += r => tcs.SetResult(_response);
            return tcs.Task;
        }
    }

I’m not sure how or why it’s doing so – the only variable it should be capturing is the TaskCompletionSource, which is intentional. Is this actually a problem and how would I go about solving it if it is?

EDIT: The warning is on the first lambda (the Timeout event).

  • 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-06-13T02:32:27+00:00Added an answer on June 13, 2026 at 2:32 am

    It seems that the problem isn’t the line I think it is.

    The problem is that I have two lambdas referencing fields in the parent object: The compiler generates a class with two methods and a reference to the parent class (this).

    I think this would be a problem because the reference to this could potentially stay around in the TaskCompletionSource object, preventing it from being GCed. At least that’s what I’ve found on this issue suggests.

    The generated class would look something like this (obviously names will be different and unpronounceable):

    class GeneratedClass {
        Request _this;
        TaskCompletionSource tcs;
    
        public lambda1 (Object e, ElapsedEventArgs a) {
            tcs.SetException(new TimeoutException("Timeout at " + a.SignalTime));
        }
    
        public lambda2 () {
            tcs.SetResult(_this._response);
        }
    }
    

    The reason the compiler does this is probably efficiency, I suppose, as the TaskCompletionSource is used by both lambdas; but now as long as a reference to one of those lambdas is still referenced the reference to Request object is also maintained.

    I’m still no closer to figuring out how to avoid this issue, though.

    EDIT: I obviously didn’t think through this when I was writing it. I solved the problem by changing the method like this:

        public Task<Response> TaskResponse
        {
            get
            {
                var tcs = new TaskCompletionSource<Response>();
    
                Timeout.Elapsed += (e, a) => tcs.SetException(new TimeoutException("Timeout at " + a.SignalTime));
    
                if (_response != null) tcs.SetResult(_response);
                else ResponseHandler += tcs.SetResult; //The event passes an object of type Response (derp) which is then assigned to the _response field.
                return tcs.Task;
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am getting this warning from FxCop: 'RestartForm' contains field 'RestartForm.done' that is of
I'm getting this ReSharper warning: Access to foreach variable in closure. May have different
I'm getting this warning from the compiler which does not make sense at all
I'm getting this warning from sccanf() : ../../../../ext/oedipus/oedipus.c:211: warning: conversion lacks type at end
I am getting this warning even though everything functions perfectly. Here is the code
Getting red warning message that expected identifier for this statement CALayer = ImageView.layer; Using
I'm getting an Implicit declaration of function 'pathInDocumentDirectory' warning for this line of code
Thanks in advance. Getting this warning when using below code: Warning: file_get_contents(test.php) [function.file-get-contents]: failed
I'm getting this warning Implicit conversion from enumeration type 'UIViewAnimationCurve' to different enumeration type
I'm getting this warning: perl: warning: Please check that your locale settings: LANGUAGE =

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.