using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;

namespace Core.Mes.ServerManager
{


    public class ExitMessageFilter : System.Windows.Forms.IMessageFilter
    {
        private FrmSeverMain frm;
        public ExitMessageFilter(FrmSeverMain frm)
        {
            this.frm = frm;
        }

        #region IMessageFilter 成员

        public bool PreFilterMessage(ref Message m)
        {
            if (frm == null)
            {

            }

            if (m.Msg == 16)
            {
                ApplicationExit();
                Thread.Sleep(2 * 1000);
            }
           
            return false;
        }


        #endregion

        private static void ApplicationExit()
        {

            string path = string.Format(@"{0}/log/Application/{1}.txt", Application.StartupPath, System.DateTime.Now.ToString("yyyy_MM_dd"));

            using (StreamWriter sw = new StreamWriter(path, true, Encoding.UTF8))
            {

                StringBuilder sbtxt = new StringBuilder();
                sbtxt.AppendLine("==============================================");
                sbtxt.AppendLine("从任务管理器中关闭服务");
                sbtxt.AppendLine(string.Format("服务关闭时间:{0}", DateTime.Now.ToString("yyyy-MM-dd HH:mm::ss")));
                sbtxt.AppendLine("==============================================");
                sw.WriteLine(sbtxt.ToString());

            }


        }
    }

}
