Exemplo n.º 1
0
        /// <summary>
        /// 写文件线程方法
        /// </summary>
        private void WriteThread()
        {
            if (!Directory.Exists("Log"))
            {
                Directory.CreateDirectory("Log");
            }
            CInfo info = null;

            while (true)
            {
                while (wirteLogs.Count > 0)
                {
                    lock (lockWirteLogs)
                    {
                        info = wirteLogs.Dequeue();
                    }

                    if (info != null)
                    {
                        string path = System.IO.Path.Combine("Log", DateTime.Now.ToString("yyyy-MM-dd")) + ".log";
                        using (StreamWriter writer = new StreamWriter(path, true))
                        {
                            writer.WriteLine(info.ToString());
                        }


                        if (Logs.Count > 0xffff)
                        {
                            Logs.RemoveRange(0, 0xff);
                        }
                        Logs.Add(info);

                        if (IsClose)
                        {
                            return;
                        }
                        this.Dispatcher.Invoke(() =>
                        {
                            ListViewItem item = new ListViewItem();
                            item.Content      = info;
                            if (info.type == CInfo.emType.Error)
                            {
                                item.Background = Brushes.Red;
                            }
                            listView.Items.Add(item);
                        });

#if DEBUG
                        //Console.WriteLine(info.ToString());
#endif
                        info = null;
                    }
                }

                Thread.Sleep(10);
            }
        }