private void DeserializeButton_Click(object sender, RoutedEventArgs e)
        {
            var listRand = new ListRand();

            using (var stream = new MemoryStream())
            {
                var writer = new StreamWriter(stream);
                writer.Write(GeneratedListTextBox.Text);
                writer.Flush();

                stream.Seek(0, SeekOrigin.Begin);

                listRand.Deserialize(stream);
            }

            PrintList(listRand, DeserializedTextBox);
        }
        private void GenerateButton_Click(object sender, RoutedEventArgs e)
        {
            var listRand = new ListRand();

            // Let's have some fun with TPL
            var nums = Enumerable.Range(0, NODES_COUNT-1).ToArray();

            // Just a balanced partitioner
            var partitioner = Partitioner.Create(nums, true);

            var nodes = partitioner.AsParallel().Select(i =>
            {
                var newNode = new ListNode
                {
                    Id = Guid.NewGuid(),
                    Data = string.Format("Node{0} Data", i)
                };

                return newNode;
            }).ToList();

            var rand = new Random(Environment.TickCount);

            ListNode previous = null;
            foreach (var node in nodes)
            {
                node.Previous = previous;
                node.Random = nodes[rand.Next(0, nodes.Count)];

                if (previous != null)
                {
                    previous.Next = node;
                }

                previous = node;
            }

            listRand.Count = nodes.Count;
            listRand.Head = nodes.First();
            listRand.Tail = nodes.Last();

            _lastListRand = listRand;

            PrintList(listRand, GeneratedListTextBox);
        }
        private void PrintList(ListRand listRand, TextBox printTo)
        {
            // Suppose Stream management code should be here, and not in serialization code.
            using (var stream = new MemoryStream())
            {
                listRand.Serialize(stream);

                stream.Seek(0, SeekOrigin.Begin);

                using (var reader = new StreamReader(stream))
                {
                    // Again, having fun with TPL
                    var serializedList = reader.ReadToEndAsync().ContinueWith((t) =>
                    {
                        printTo.Text = t.Result;
                    }, TaskScheduler.FromCurrentSynchronizationContext());
                }
            }
        }