1

新蜂警报。问题:我填充了一个组合框,用户进行了选择。然后我创建并启用 FSW。一切正常,直到用户重新访问组合框以进行替代选择。此时,另一个 FSW 被实例化,导致基于“文件正在使用”错误的 IO 异常。当用户在组合框中进行后续选择时,我需要关闭 FSW(或销毁实例化)。整个程序由带有组合框的 Winform 驱动。

当用户重新访问组合框并进行另一个选择时,如何打开/关闭 FSW,或破坏 FSW 实例并允许创建一个新的、类似的?

调用 FSW 实例化的代码:

        private void MinAndGo()
    {
        if (strLblPtr != null)
        {
            if(strLblPtr != "None")
            {
                if (!CheckMyPrinter(strLblPtr))
                {
                    MessageBox.Show(ForegroundWindow.Instance, "Printer is not ready. Make sure it's turned on "
                    + "and has paper loaded.", "Printer Not Ready");
                }
            }
            this.WindowState = FormWindowState.Minimized;
            this.Activate();
            bCreateWatcher = true;
            Watchit();
        }
    }

WatchIt() 的代码。我打算使用 bool bCreateWatcher 来打开和关闭 FSW。

private static void Watchit()
    {
        List<string> list = new List<string>();
        list.Add("C:\\SAMMS\\pcl");
        list.Add("C:\\SAMMS\\lbl");
        foreach (string my_path in list)
        {
            Watch(my_path);
        }
    }
    private static void Watch(string watch_folder)
    {

        FileSystemWatcher watcher = new FileSystemWatcher();
        watcher.InternalBufferSize = 8192; //defaults to 4KB, need 8KB buffer
        watcher.Path = watch_folder;
        watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
       | NotifyFilters.FileName | NotifyFilters.DirectoryName;
        watcher.Filter = "*.*"; 
        watcher.Created += new FileSystemEventHandler(OnCreated);
        // Begin watching.
        try
        {
            if (bCreateWatcher)
            {
                watcher.EnableRaisingEvents = true;
            }
            else
            {
                watcher.EnableRaisingEvents = false;
            }
        }
        catch(Exception ex)
        {
            MessageBox.Show(ForegroundWindow.Instance, "FSW not set correctly" + ex, "FSW Error");
        }
     }
4

2 回答 2

1

FileSystemWatcher工具IDisposable。_ 因此,您应该调用Dispose来销毁实例。

您可以在这里找到更多信息:

于 2013-02-11T19:42:16.487 回答
0

好的,所以看起来你需要将你的观察者存储在某个地方,也许是一个在路径上键入的字典?您还需要拥有所有包含在 implement 中的类IDisposable,以便您可以正确调用Dispose()您当前打开的任何观察者,并且该类已被处置。(然后您应该确保包含的类也被正确处理。)

我会重构Watch()为这样的东西(可能会更好):

private static IDictionary<string, FileSystemWatcher> _openWatchers
    = new Dictionary<string, FileSystemWatcher>();

private static void Watch(string watch_folder)
{
    if (!bCreateWatcher)
    {
        if (_openWatchers.ContainsKey(watch_folder))
        {
            _openWatchers[watch_folder].Dispose();
            _openWatchers.Remove(watch_folder);
        }
        return;
    }

    FileSystemWatcher watcher = new FileSystemWatcher();
    watcher.InternalBufferSize = 8192; //defaults to 4KB, need 8KB buffer
    watcher.Path = watch_folder;
    watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
   | NotifyFilters.FileName | NotifyFilters.DirectoryName;
    watcher.Filter = "*.*"; 
    watcher.Created += new FileSystemEventHandler(OnCreated);
    // Begin watching.
    try
    {
         watcher.EnableRaisingEvents = true;
         _openWatchers[watch_folder] = watcher;
    }
    catch(Exception ex)
    {
        MessageBox.Show(ForegroundWindow.Instance, "FSW not set correctly" + ex, "FSW Error");
    }
 }

你的Dispose()方法:

 public void Dispose()
 {
     foreach (FileSystemWatcher fsw in _openWatchers.Values)
     {
          fsw.Dispose();
     }
 }
于 2013-02-12T20:19:21.990 回答