עליך להתחבר בכדי להוריד קבצים מהאתר
על מנת להבטיח שמשאבינו לא ינוצלו לרעה.
Asynchronous Delegates |
1 : using System;
2 : using System.Collections.Generic;
3 : using System.Linq;
4 : using System.Text;
5 : using System.Threading.Tasks;
6 :
7 : namespace DelegateSample01
8 : {
9 : public delegate void MyDelegate();
10 : class Program
11 : {
12 : static void Main(string[] args)
13 : {
14 : MyDelegate my_delegate = new MyDelegate(Func);
15 : //MyDelegate my_delegate2 = Func;
16 : my_delegate();
17 : Console.WriteLine("Main - Thread ID: {0}", System.Threading.Thread.CurrentThread.ManagedThreadId);
18 : }
19 : public static void Func()
20 : {
21 : Console.WriteLine("Delegate - Thread ID: {0}", System.Threading.Thread.CurrentThread.ManagedThreadId);
22 : }
23 : }
24 :
25 :
26 : }
1 : using System;
2 : using System.Collections.Generic;
3 : using System.Linq;
4 : using System.Text;
5 : using System.Threading.Tasks;
6 :
7 : namespace DelegateSample02
8 : {
9 : public delegate void MyDelegate(string msg, int num);
10 : class Program
11 : {
12 : static void Main(string[] args)
13 : {
14 : MyDelegate del1 = new MyDelegate(Func);
15 : MyDelegate del2 = new MyDelegate(Func);
16 :
17 : IAsyncResult result1 = del1.BeginInvoke("Func1", 100, null, null);
18 : IAsyncResult result2 = del2.BeginInvoke("Func2", 100, null, null);
19 : Console.WriteLine("Main - Thread ID: {0}", System.Threading.Thread.CurrentThread.ManagedThreadId);
20 :
21 : result1.AsyncWaitHandle.WaitOne();
22 : result2.AsyncWaitHandle.WaitOne();
23 : }
24 : public static void Func(string msg, int num)
25 : {
26 : Console.WriteLine("Delegate - Thread ID: {0}", System.Threading.Thread.CurrentThread.ManagedThreadId);
27 : }
28 : }
29 : }
1 : using System;
2 : using System.Collections.Generic;
3 : using System.Linq;
4 : using System.Text;
5 : using System.Threading.Tasks;
6 :
7 : namespace DelegateSample03
8 : {
9 : public delegate void MyDelegate(string msg, int num);
10 : class Program
11 : {
12 : static void Main(string[] args)
13 : {
14 : MyDelegate del1 = new MyDelegate(Func);
15 : MyDelegate del2 = new MyDelegate(Func);
16 :
17 : IAsyncResult result1 = del1.BeginInvoke("Func1", 10,
18 : new AsyncCallback(TaskCompleted), null);
19 : IAsyncResult result2 = del2.BeginInvoke("Func2", 10,
20 : new AsyncCallback(TaskCompleted), null);
21 : del1.EndInvoke(result1);
22 : del2.EndInvoke(result2);
23 : Console.WriteLine("Continue .....");
24 : }
25 : public static void Func(string msg, int num)
26 : {
27 : for (int i = 0; i < num; i++)
28 : {
29 : Console.Write(msg + ": " + i + ", ");
30 : System.Threading.Thread.Sleep(100);
31 : }
32 : }
33 : public static void TaskCompleted(IAsyncResult R)
34 : {
35 : Console.WriteLine("End Async");
36 : }
37 : }
38 : }
1 : using System;
2 : using System.Collections.Generic;
3 : using System.Linq;
4 : using System.Text;
5 : using System.Threading.Tasks;
6 : using System.Runtime.Remoting.Messaging;
7 :
8 : namespace DelegateSample04
9 : {
10 : public delegate int MyDelegate(int[] arr);
11 : class Program
12 : {
13 : static void Main(string[] args)
14 : {
15 : int[] arr1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 56, 44, 5, 12, 83, 56, 22, 92 };
16 : int[] arr2 = { 11, 22, 33, 44, 55, 66, 77, 88, 99 };
17 :
18 : MyDelegate del1 = new MyDelegate(Sum);
19 : IAsyncResult result1 = del1.BeginInvoke(arr1, new AsyncCallback(MyCallBack), "A message from thread 1");
20 :
21 : MyDelegate del2 = new MyDelegate(Sum);
22 : IAsyncResult result2 = del2.BeginInvoke(arr2, new AsyncCallback(MyCallBack), "A message from thread 2");
23 :
24 : int sum1 = del1.EndInvoke(result1);
25 : int sum2 = del2.EndInvoke(result2);
26 : Console.WriteLine("Sum1 = {0}, Sum2 = {1}", sum1, sum2);
27 : }
28 : public static int Sum(int[] arr)
29 : {
30 : int sum = 0;
31 : for (int i = 0; i < arr.Length; i++)
32 : {
33 : sum += arr[i];
34 : }
35 : return sum;
36 : }
37 : static void MyCallBack(IAsyncResult async)
38 : {
39 : Console.WriteLine(async.AsyncState.ToString());
40 : }
41 : }
42 : }