using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using Core.Mes.ClientFrameWork;
using System.IO;
using System.Configuration;
using System.Diagnostics;
using System.Data;

namespace DataTest
{
    class Program
    {
        static void Main(string[] args)
        {


            //Thread thread1 = new Thread(new ThreadStart(GetTestData));
            //thread1.Start();
            //Console.WriteLine("线程1开始运行...");
            //Thread thread2 = new Thread(new ThreadStart(GetTestData));
            //thread2.Start();
            //Console.WriteLine("线程2开始运行...");
            //Console.Read();

            string url = ConfigurationManager.AppSettings["RemoteServerUrl"];
            ClientCommon._RemotingHelp.InitServerUrlList("ServerCommon", url);
            arevent = new AutoResetEvent(false);
            timer.Elapsed += new System.Timers.ElapsedEventHandler(Timer_ElapsedEvent);
            timer.Interval = 10 * 1000;
            timer.Start();
            Console.Read();

            //#region 多线程
            //for (int i = 0; i < 7; i++)
            //{
            //    int a = i;
            //    count = i;
            //    ThreadPool.QueueUserWorkItem(new WaitCallback(delegate { GetTestData(a); }));

            //}

            
            //Console.Read();

            //#endregion

        }
        private static int j = 0;
        private static void Timer_ElapsedEvent(Object obj, System.Timers.ElapsedEventArgs args)
        {
            #region 多线程
            for (int i = 0; i < 5; i++)
            {
                int a = i;
                count = i;
                ThreadPool.QueueUserWorkItem(new WaitCallback(delegate { GetTestData(a); }));

            }

            //j++;
            //if (j == 1)
            //    timer.Stop();
            Console.Read();

            #endregion
        }


        private static int count = 0;
        private static int errnum = 0;
        static AutoResetEvent arevent = null;
        static System.Timers.Timer timer=new System.Timers.Timer();

        
        private static void GetTestData(Object a)
        {


            if (count == 4)
                arevent.Set();
            else
            {
                arevent.WaitOne();
            }


            try
            {

                Stopwatch watch = new Stopwatch();
                watch.Start();

                string strOut = "";
                DataSet obj = ClientCommon._RemotingHelp.ExecuteMethod("ServerCommon", "Core.Mes.ServerCommon.UserInfoManager", "SelectTestUser", null, out strOut) as DataSet;
                if (strOut != "")
                {
                    Console.WriteLine("失败:" + strOut);
                    errnum++;
                    Console.WriteLine("连接失败次数：{0}", errnum);
                }

                Console.WriteLine("");
                Console.WriteLine("线程{0}开始运行...{1}", a, DateTime.Now.ToString());
                watch.Stop();
                if (watch.Elapsed.Milliseconds == 30000)
                {
                    Console.WriteLine("执行超时。。。");
                    return;
                }
                Console.WriteLine("线程{0}运行时间：{1}ms", a, watch.ElapsedMilliseconds.ToString());
                if (obj != null && obj.Tables.Count > 0)
                {
                    //Console.WriteLine("数据：{0}\r\n",obj.Tables[0].Rows[0][0].ToString());
                    Console.WriteLine("数目：" + obj.Tables[0].Rows.Count.ToString() + "\r\n");
                    obj.Clear();
                    obj.Dispose();
                    GC.Collect();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("出错啦，跑不动啦~："+ex.Message);
            }
            



          
             
        }

        
    }
}
