现在的位置: 首页 > 综合 > 正文

为什么C#的StreamWriter.Close()会引起windows平台上的关闭程序的问题?

2013年09月16日 ⁄ 综合 ⁄ 共 663字 ⁄ 字号 评论关闭

我用c#时写了一个类用来保存日志,大概的做法是:

// write logs to file
    public class LogInfo
    {
        static public LogInfo getInstance() {
            if (pLogInfo == null)
                pLogInfo = new LogInfo();

            return pLogInfo;
        }

        private LogInfo()
        {
            iLine = 0;
            logFile = new StreamWriter(@"Gobang.log", true);
            logFile.AutoFlush = true;
        }

        ~LogInfo()
        {
            // why this operation can cause something that Windows regards as a problem?
            logFile.Close();
        }

        public void WriteLog(string strInfo)
        {
            logFile.WriteLine(iLine + " " + strInfo);
            iLine++;
        }
        public void WriteLogFlush(string strInfo)
        {
            WriteLog(strInfo);
        }

        private static int iLine;
        private StreamWriter logFile;

        static private LogInfo pLogInfo;
    }

因为在析构时关闭StreamWriter的操作,每次在关闭该windows form的程序时windows都提示该程序正在关闭,并且调用“find a solution to the problem..."的处理机制。有人知道是为什么吗?

抱歉!评论已关闭.