0

I'm working with TaskCompletionSource. I register event on object, but when I try to unregister in my event method, resharper underlines it with note: Access to modified closure.

here is my code

var taskCompletionSource = new TaskCompletionSource<bool>();
OnConnectionStateChangedInd handlerConnectionStateChangedInd = null;
OnBootCompletedCnfCallback handlerBootCompletedCnfCallback = null;

handlerConnectionStateChangedInd = (id, method, addr, port, nick) =>
{
    _corePttObject.onConnectionStateChangedInd -= handlerConnectionStateChangedInd;
    _connectionState = id;

    taskCompletionSource.SetResult(true);
};
_corePttObject.onConnectionStateChangedInd += handlerConnectionStateChangedInd;

This line is underlined:

_corePttObject.onConnectionStateChangedInd -= handlerConnectionStateChangedInd;

Here is my complete definition of method:

public Task<LoginResult> LoginAsync(string address)
    {
        var taskCompletionSource = new TaskCompletionSource<LoginResult>();
        OnUserAcceptCertWithNamePasswInd handlerAcceptCertWithNamePasswInd = null;
        OnAppExLoginProtocolServiceCnf handlerAppExLoginProtocolServiceCnf = null;
        handlerAcceptCertWithNamePasswInd = (cert, caCert, rootCert, hash, pos, data) =>
        {
            var loginCompletedArgs = new LoginResult
            {
                SvrCertificate = ParseCertificate(cert),
                CaCertificate = ParseCertificate(caCert),
                RootCertificate = ParseCertificate(rootCert),
                CertificateHash = hash,
                GridPosition = pos,
                GridData = data
            };
            _corePttObject.onUserAcceptCertWithNamePasswInd -= handlerAcceptCertWithNamePasswInd;
            taskCompletionSource.SetResult(loginCompletedArgs);
        };

        handlerAppExLoginProtocolServiceCnf = (nick, result, cause, link) =>
        {
            _corePttObject.onAppExLoginProtocolServiceCnf -= handlerAppExLoginProtocolServiceCnf;
        };

        _corePttObject.onAppExLoginProtocolServiceCnf += handlerAppExLoginProtocolServiceCnf;
        _corePttObject.onUserAcceptCertWithNamePasswInd += handlerAcceptCertWithNamePasswInd;
        //TODO: read id.
        _corePttObject.Login(address, true, "ID");

        return taskCompletionSource.Task;
    }
JuP
  • 535
  • 1
  • 6
  • 23

2 Answers2

1

If you click click on the lightbulb suggestion, there will be an option "Why is ReSharper suggesting this". If you click on it, it will take you to this useful explanation.

I'd need more context to tell if this potential trap can actually hurt you in your particular case.

You can also check out this answer here.

Community
  • 1
  • 1
Gediminas Masaitis
  • 3,172
  • 14
  • 35
1

The reason for this warning is: for handlerConnectionStateChangedInd you declare an anonymous method that - when eventually executed - will access/change _corePttObject.onConnectionStateChangedInd.

Then, after this declaration, you change _corePttObject.onConnectionStateChangedInd already in this line:

_corePttObject.onConnectionStateChangedInd += handlerConnectionStateChangedInd;

So ReSharper is warning you that the value _corePttObject.onConnectionStateChangedInd is different between the time you declare handlerConnectionStateChangedInd and the time it is executed.

René Vogt
  • 43,056
  • 14
  • 77
  • 99