Created
May 17, 2015 14:52
-
-
Save Aracturat/368020964e82e396579d to your computer and use it in GitHub Desktop.
Closure Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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