using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace SimonTestThread
{
    // define the delegate
    public delegate void TicketSoldFinished();
    public class SimonTicketShop
    {
        public SimonTicketShop(int ticketCount)
        {
            m_TicketCount = ticketCount;
            m_OnTicketSoldFinished = null;
        }
        public void SellTheTicket()
        {
            for(int i= m_TicketCount; i>0; --i)
            {
                Console.WriteLine("{0}", i);
                System.Threading.Thread.Sleep(500);
            }
            if(m_OnTicketSoldFinished != null)
            {
                m_OnTicketSoldFinished();
            }
            return;
        }
        public TicketSoldFinished m_OnTicketSoldFinished;
        private int m_TicketCount;
    }
    public class ShopManager
    {
        public ShopManager()
        {
        }
        public void AllTicketsAreSold()
        {
            Console.WriteLine("All the tickets are sold. :-)");
            return;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            ShopManager shopManager = new ShopManager();
            SimonTicketShop simonTicketShop = new SimonTicketShop(20); //20 tickets initially
            simonTicketShop.m_OnTicketSoldFinished += shopManager.AllTicketsAreSold;
            Thread aThread = new Thread(new ThreadStart(simonTicketShop.SellTheTicket));
            aThread.Start();
            // Main thread also can do other things.
            aThread.Join();
        }      
    }
}