1. 程式人生 > >為什麼C#的StreamWriter.Close()會引起windows平臺上的關閉程式的問題?

為什麼C#的StreamWriter.Close()會引起windows平臺上的關閉程式的問題?

我用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..."的處理機制。有人知道是為什麼嗎?