Skip to content

Instantly share code, notes, and snippets.

@Aracturat
Created May 17, 2015 14:52
Show Gist options
  • Save Aracturat/368020964e82e396579d to your computer and use it in GitHub Desktop.
Save Aracturat/368020964e82e396579d to your computer and use it in GitHub Desktop.
Closure Test
using System;
using System.Threading;
namespace DelegateTest
{
class Algorithm
{
public int Key { get; set; }
}
class Program
{
private delegate void Crypt(int i, Algorithm algoritm);
static void Main(string[] args)
{
Crypt[] funcs = new Crypt[10];
for (int i = 0; i < funcs.Length; i++)
{
funcs[i] = Encrypt;
}
Algorithm alg = new Algorithm();
alg.Key = 15;
IAsyncResult[] asr = new IAsyncResult[10];
for (int j = 0; j < 10; j++)
{
asr[j] = funcs[j].BeginInvoke(j, alg, null, null);
}
while (!asr[0].IsCompleted)
{
}
}
private static void Encrypt(int i, Algorithm algorithm)
{
Thread.Sleep((10 - i) * 1000);
Console.WriteLine("{0} {1}", i, algorithm.Key);
algorithm.Key = i;
Console.WriteLine("{0} {1}", i, algorithm.Key);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment