Array.SyncRoot Property: synchronize access to an array.
using System;
using System.Threading;
public class Starter {
public static void Main() {
Array.Sort(zArray);
Thread t1 = new Thread(new ThreadStart(DisplayForward));
Thread t2 = new Thread(new ThreadStart(DisplayReverse));
t1.Start();
t2.Start();
}
private static int[] zArray = { 1, 5, 4, 2, 4, 2, 9, 10 };
public static void DisplayForward() {
lock (zArray.SyncRoot) {
Console.Write("\nForward: ");
foreach (int number in zArray) {
Console.Write(number);
}
}
}
public static void DisplayReverse() {
lock (zArray.SyncRoot) {
Array.Reverse(zArray);
Console.Write("\nReverse: ");
foreach (int number in zArray) {
Console.Write(number);
}
Array.Reverse(zArray);
}
}
}
Related examples in the same category