Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# StreamWriter在访问方法时被释放_C#_.net_Class - Fatal编程技术网

C# StreamWriter在访问方法时被释放

C# StreamWriter在访问方法时被释放,c#,.net,class,C#,.net,Class,当我尝试使用方法写入日志文件时,我遇到了一个问题,即我声明的StreamWriter被释放。除了在另一个类中运行AttachPink或AttachBlue外,所有操作都按预期进行。然后StreamWriter被释放,我得到一个nullPointerException class Logs : IDisposable { //other declarations private StreamWriter HistoryWriter; pr

当我尝试使用方法写入日志文件时,我遇到了一个问题,即我声明的StreamWriter被释放。除了在另一个类中运行AttachPink或AttachBlue外,所有操作都按预期进行。然后StreamWriter被释放,我得到一个nullPointerException

class Logs : IDisposable
    {

        //other declarations

        private StreamWriter HistoryWriter;
        private int ReportInterval = 0;

        public void NewHistory()
        {
            HistoryWriter = new StreamWriter(HistoryLocation + HistoryName + HistoryExtension);
            PrepareHistory();
        }

        private void PrepareHistory()
        {
            HistoryWriter.WriteLine("<html><body bgcolor='#000000'>");
            /*
             *  Insert initial HTML tags 
             */
        }

        public void SendHistory()
        {
            HistoryWriter.WriteLine("</body></html>");
            /*
             *  Final HTML tags
             */ 
            HistoryWriter.Close();
            if (ReportInterval > 0)
            {
                /*
                 *  Upload File
                 */
            }
            else
            {
                Debug.WriteLine("ERROR: Report Interval for History has not been set");
            }
            NewHistory();
        }

        public void AttachPink(String message, StreamWriter writer)
        {
            writer.Write(
                "<font color='DA1BE0'>" 
                + message
                + "</font>");
        }

        public void AttachBlue(String message, StreamWriter writer)
        {
            writer.Write(
                "<font color='0C93ED'>" 
                + message
                + "</font>");
        }

        public StreamWriter getHistoryWriter()
        {
            return HistoryWriter;
        }

        public void SetHistoryInterval(int interval)
        {
            ReportInterval = interval;
        }

        public void Dispose()
        {
            if (HistoryWriter != null)
            {
                HistoryWriter.Close();
                HistoryWriter.Dispose();
                HistoryWriter = null;
            }
        }

    }

我不知道在访问多个方法时应该如何保存类变量状态。

我想您需要的是单例模式()

我的一个简单实现,您可以在每次需要单例时重用它

public class Singleton<T> where T : class, new()
{
    private static object sync = null;
    private static volatile T i;
    protected Singleton() { }

    public static T I
    {
        get
        {
            if (i == null)
                lock (sync)
                    if (i == null)
                        i = new T();

            return i;
        }
    }
}

希望这有帮助:)

我这样做是为了解决所描述的问题,但显然不起作用。然后,您应该将代码简化为更合理的代码。理想情况下,制作一个简短但完整的程序来演示问题。我怀疑您还没有向我们展示一些相关的代码。我不认为所展示的代码中存在bug。不管怎样,如果有人给你指出错误,它对你没有任何帮助。我强烈建议您借此机会学习热调试程序。在你发现问题之前不要盯着代码看。设置断点并使用F10。似乎您正在关闭调用代码中的流(可能通过
使用
子句?)?如图所示的代码虽然不常见(并且遵循非典型C#编码约定),但没有任何明显的方式导致您所显示的行为。使用日志的
类的语法根本无效。。。不确定在哪里使用它。@blacblu为什么在输入方法时它突然为空。也许你看到的是另一个变量?将鼠标悬停在不同的变量上以查看其值。
public class Singleton<T> where T : class, new()
{
    private static object sync = null;
    private static volatile T i;
    protected Singleton() { }

    public static T I
    {
        get
        {
            if (i == null)
                lock (sync)
                    if (i == null)
                        i = new T();

            return i;
        }
    }
}
class Logs : Singleton<Logs>
{
... your code goes here
}
Logs.I.AttachBlue(...);