Skip to content

Instantly share code, notes, and snippets.

@mrtank
Last active May 3, 2016 15:49
Show Gist options
  • Save mrtank/db049b37bae1bdf788f36693ebd6c0e4 to your computer and use it in GitHub Desktop.
Save mrtank/db049b37bae1bdf788f36693ebd6c0e4 to your computer and use it in GitHub Desktop.
[TestMethod]
public void BeginEndPairWithData()
{
byte[] changingData = new byte[] { 0 };
Action changeData = () => changingData = new byte[] { 1 };
IAsyncResult res = changeData.BeginInvoke(changeData.EndInvoke, null);
res.AsyncWaitHandle.WaitOne();
changingData.ShouldAllBeEquivalentTo((byte)1);
}
[TestMethod]
public void BeginEndPairWithDataInSeparateMethods()
{
byte[] changingData = new byte[] { 0 };
IAsyncResult res = BeginChange(ref changingData, EndChange);
res.AsyncWaitHandle.WaitOne();
changingData.ShouldAllBeEquivalentTo((byte)1);
}
private void EndChange(IAsyncResult res)
{
}
private IAsyncResult BeginChange(ref byte[] changingData, AsyncCallback callback)
{
Action changeData = () => changingData = new byte[] { 1 }; // <-- problem here Can't change changingData in lambda
return changeData.BeginInvoke(callback, null);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment