using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ServerHelp
{
    internal class Program
    {
        static void Main(string[] args)
        {
            var a = new A { name = "ddd" };
            FindDuplicate(a);
            Console.WriteLine(a.name);
            return;
            string _path = @"F:\FEST.txt";

            Thread thread = new Thread(() =>
            {
                FileStream fs = new FileStream(_path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
                StreamWriter sr = new StreamWriter(fs, System.Text.Encoding.UTF8);
                for (int i = 0; i < 1000; i++)
                {
                    sr.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                    sr.Flush();
                    Thread.Sleep(100);
                }
            });
            Thread thread2 = new Thread(() =>
            {
                FileStream fs = new FileStream(_path, FileMode.Open, FileAccess.ReadWrite,FileShare.ReadWrite);
                StreamReader sr = new StreamReader(fs, System.Text.Encoding.UTF8);
                bool wu = false;
                while (true)
                {
                    var str = sr.ReadToEnd();
                    Thread.Sleep(100);
                    if (str.Length > 0)
                        Console.WriteLine(DateTime.Now + " ----- " + str.Replace(Environment.NewLine, "").Substring(0, "yyyy-MM-dd HH:mm:ss.fff".Length));
                    else
                    {
                        if (!wu)
                        {
                            Console.WriteLine("wu");
                            wu = true;
                        }
                    }
                }

            });
            thread.Start();
            thread2.Start();
            return;
            while (true)
            {
                

                try
                {
                    string path = @"C:\wwwroot\App_Data\Server\Server.xcljj";
                    while (true)
                    {
                        var server = ServiceController.GetServices().FirstOrDefault(service => service.ServiceName == "BLV RUCS");
                        if (server == null)
                        {
                            Console.WriteLine("服务不存在！");
                            Thread.Sleep(1000);
                            continue;
                        }
                        if (!File.Exists(path))
                        {
                            Thread.Sleep(1000);
                            Console.WriteLine(server.Status.ToString());
                            continue;
                        }
                        int type = int.Parse(File.ReadAllText(path));
                        File.Delete(path);
                        Console.WriteLine(DateTime.Now.ToString() + (type == 1 ? "启动服务" : (type == 2 ? "重启" : "停止")) + "服务");
                        switch (type)
                        {
                            case 1:
                                if (server.Status != ServiceControllerStatus.Running)
                                {
                                    server.Start();
                                }
                                break;
                            case 2:
                                if (server.Status == ServiceControllerStatus.Running)
                                {
                                    server.Stop();
                                    while (ServiceController.GetServices().FirstOrDefault(service => service.ServiceName == "BLV RUCS").Status != ServiceControllerStatus.Stopped)
                                    {

                                    }
                                }
                                server.Start();
                                break;
                            case 3:
                                server.Stop();
                                break;
                        }

                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }
            Console.ReadLine();
        }

        public class A
        {
            public string name { get; set; }
        }
        public static void FindDuplicate(A s) { s.name = "sss"; }
        public static int FindDuplicate(int[] arr)
        {
            int index = 0;
            int res = arr[0];
            int count = arr.Length - 1;
            for (int i = 1; i <= count; i++)
            {
                if (res == arr[i])
                {
                    break;
                }

                if (count == i && index != count)
                {
                    index++;
                    res = arr[index];
                    // 下次循环是 index + 1
                    i = index;
                }
            }
            return res == arr[0] ? -1 : res;
        }
    }
}
