using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;
using System.Collections;
namespace AutoDisconnect
{
    class Program
    {
        static Object door = new object();
        static ServerConnection conn = null;
        static Server server = null;
        static int SleepTime = 0;
        static ArrayList results = new ArrayList();
        static int thdCount = 200;
        static void WorkProc(Object obj)
        {
            int pass = (int)obj;
                int dbCount = server.Databases.Count;
                for (int index = 0; index < dbCount; index++)
                {
                    String name = String.Empty;
                    lock (door)
                    {
                        name = server.Databases[index].Name;
                        results.Add(pass.ToString() + " " + name);
                        conn.Disconnect();
                    }
                    System.Threading.Thread.Sleep(SleepTime);
                    if (index == (pass % dbCount))
                    {
                        lock (door)
                        {
                            foreach (Table table in server.Databases[index].Tables)
                            {
                                name = table.Name;
                                results.Add("--" + server.Databases[index].Name + " " + pass.ToString() + " table" + name);
                                conn.Disconnect();
                            }
                        }
                    }
                    System.Threading.Thread.Sleep(SleepTime);
                }
        }
        static void Main(string[] args)
        {
            conn = new ServerConnection();
            conn.ApplicationName = "test1";
            Console.WriteLine(conn.AutoDisconnectMode);
            Console.WriteLine(conn.MinPoolSize);
            Console.WriteLine(conn.MaxPoolSize);
            Console.WriteLine(conn.NonPooledConnection);
            Console.WriteLine(conn.PooledConnectionLifetime);
            //conn.AutoDisconnectMode = AutoDisconnectMode.NoAutoDisconnect;
            conn.AutoDisconnectMode = AutoDisconnectMode.DisconnectIfPooled;
            conn.PooledConnectionLifetime = 3;
            conn.MinPoolSize = 3;
            conn.MaxPoolSize = 100;
            conn.NonPooledConnection = false;
      
            //conn.Connect();
            //conn.Connect();
            //conn.Connect();
            //conn.Connect();
            //conn.Connect();
            //conn.Disconnect();

            //conn.Connect();
            Console.WriteLine(conn.PooledConnectionLifetime);
            //System.Threading.Thread.Sleep(0);
            Console.WriteLine(conn.NonPooledConnection);
            Console.WriteLine(conn.ProcessID);
            //conn.Disconnect();
            server = new Server(conn);
            System.Threading.Thread[] thds =
                new System.Threading.Thread[thdCount];
            for (int index = 0; index < thds.Length; index++)
            {
                thds[index] = new System.Threading.Thread(new System.Threading.
                    ParameterizedThreadStart(WorkProc));
            }
            int dbCount = server.Databases.Count;
            for (int index = 0; index < thds.Length; index++)
            {
                thds[index].Start(index);
            }
            String name = String.Empty;
            for (int index = 0; index < dbCount; index++)
            {
                lock (door)
                {
                    name = server.Databases[index].Name;
                    conn.Disconnect();
                    results.Add("M " + name);
                    System.Threading.Thread.Sleep(SleepTime);
                    int tcount = server.Databases[index].Tables.Count;
                }
            }
            Console.WriteLine(conn.ApplicationName);
            //conn.Disconnect();
            Console.WriteLine(conn.DatabaseName);
            //conn.Disconnect();
            Console.WriteLine(conn.ServerInstance);
            //conn.Disconnect();
            foreach (System.Threading.Thread thd in thds)
            {
                thd.Join();
            }
            foreach (string s in results)
            {
                Console.WriteLine(s);
            }
            Console.ReadLine();
            conn.Disconnect();
            Console.ReadLine();
        }
    }
}
