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;
}