Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9298733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T22:23:37+00:00 2026-06-18T22:23:37+00:00

Newbee alert. Problem: I populate a combo box, user makes a selection. I then

  • 0

Newbee alert. Problem: I populate a combo box, user makes a selection. I then create and enable a FSW. All works well, until user revisits combo box to make an alternate selection. At that point, another FSW is instantiated resulting in IO Exceptions based on ‘file in use’ errors. I need to switch off the FSW (or destroy the instantiation) when the user makes a subsequent selection in the combo box. Entire program is driven from a Winform with the combo box.

How do either toggle the FSW on/off, or destroy the FSW instantiation and allow a new, similar one to be created when the user revisits the combo box and makes another selection?

Code that calls for instantiation of the 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();
        }
    }

Code for WatchIt(). I was intending on using the bool bCreateWatcher to toggle the FSW on and off.

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");
        }
     }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T22:23:39+00:00Added an answer on June 18, 2026 at 10:23 pm

    Ok, so it looks like you need to store your watcher somewhere, perhaps a dictionary keyed on the path? You’ll also need to have the class that this is all contained in implement IDisposable, so that you can properly call Dispose() on any watchers you currently have open with the class is disposed of. (You should then ensure that the containing class is also properly disposed.)

    I would refactor Watch() to something like this (could probably be better):

    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");
        }
     }
    

    And your Dispose() method:

     public void Dispose()
     {
         foreach (FileSystemWatcher fsw in _openWatchers.Values)
         {
              fsw.Dispose();
         }
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a problem with a form to create a user... this is my
(Newbie DBUnit question Alert!) It appears that DBUnit for each table 'deletes all the
I am a newbee to solr.I am having a problem with adding fields/metadata to
Greetings all, I'm an AJAX newbie, and I'm having a problem with same origin
I create a user control in ASP.NET (.ascx) that combines ArcGIS Map, and several
NEWBIE ALERT! Here's the situation. I've got an Android ListActivity class (AppWindow) that contains
[Android Newbie alert] I need to capture the contents of a WebView in a
Newbee question here - I have this method in the view controller m: -(void)
I am a newbee with javascript and have the following code: <html> <body> <object
I am newbee with PHP and MySQL and need help... I have two tables

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.