为什么列表中的引用在此线程程序中丢失?

时间:2015-03-29 17:52:28

标签: c# object memory reference

我用C#编写程序,必须作为服务执行。

在我的主类中,我有class1的object1和一个Thread t,我实例化object1并使用线程t来运行方法"运行"此对象在此主类的onStart方法上(object1是一个类变量)。在onStop方法中,我调用object1的stop()方法。

在class1中我有一个列表,在class1的方法run()中我向列表中添加了一个元素(我已检查过它是否被添加)。在方法stop()中,我尝试检索此元素,但列表中没有元素。

partial class ServiceClass: ServiceBase
{
    volatile Class1 object1 = new Class1();
    volatile Thread t = null;

    public ServiceClass()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
        try
        {
            t = new Thread(object1.run);
            t.Start();
        }
        catch (Exception e)
        {
            t.Join();
        }
    }

    protected override void OnStop()
    {
        object1.Stop();
        t.Join();
    }
}

class Class1
{
    private List<FileProcessor> fileProcessors = new List<FileProcessor>();
    private FileSystemWatcher awatcher = null;

    public Class1()
    {
    }

    public void run()
    {
        FileProcessor fp = new FileProcessor();
        fileProcessors.Add(fp);

        awatcher = new FileSystemWatcher("C:\\Example", "*.csv");
        awatcher.IncludeSubdirectories = false;
        awatcher.Created += new FileSystemEventHandler(awatcher_Created); 
        awatcher.Renamed += new RenamedEventHandler(awatcher_Renamed);
        awatcher.InternalBufferSize = 4096 * 500;
        awatcher.EnableRaisingEvents = true;

    }

    public void Stop()
    {
        foreach (FileProcessor fileProcessor in this.fileProcessors)
        {
            if (fileProcessor != null)
            {
                fileProcessor.Stop();
            }
        }

        if (awatcher != null)
        {
            awatcher.EnableRaisingEvents = false;
            awatcher.Dispose();
        }
        Environment.Exit(-2);
    }

    void awatcher_Renamed(object sender, RenamedEventArgs e)
    {
        if (e.OldName.Replace(".prepare", "").Equals(e.Name) && e.Name.EndsWith(".csv") && !e.Name.Contains(ConfigurationManager.AppSettings["TestParameter"]))
        {
            FileInfo fi = new FileInfo(e.FullPath);
            awatcher_Created(sender, new FileSystemEventArgs(WatcherChangeTypes.Created, fi.DirectoryName, e.Name));
        }
    }

    void awatcher_Created(object sender, FileSystemEventArgs e)
    {
        // Nothing yet
    }


}

当调用Stop()函数时,awatcher为null且列表为空,就好像它没有被调用一样。为什么这个引用会丢失?

编辑:如果我将这些变量声明为静态,它就可以正常工作。但是,我更喜欢使用非静态变量。

1 个答案:

答案 0 :(得分:1)

好的,所以我试图重新创建你的场景。 Here it is as a Fiddle。不幸的是,我无法重现丢失的列表项。

using System;
using System.Threading;
using System.Collections.Generic;

public class MyMainClass
{
    public static Class1 object1 = new Class1();
    public static void Main()
    {
        Console.WriteLine("Mimic a service's OnStart and OnStop");
        OnStart();
        Console.WriteLine("Press any key to stop");
        Console.ReadLine();
        OnStop();
    }

    public static void OnStart()
    {
        Console.WriteLine("Started");
        Thread t = new Thread(new ThreadStart(object1.Run));
        t.Start();
    }

    public static void OnStop()
    {
        object1.Stop();
        Console.WriteLine("Stopped");
    }
}

public class Class1
{
    public List<object> list = new List<object>();
    public void Run()
    {
        list.Add("Some Element");
    }

    public void Stop()
    {
        Console.WriteLine(list[0]);
    }
}