Thread: Filesystemwatcher - is this the best approach?

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    32

    Filesystemwatcher - is this the best approach?

    Hello guys,
    i know some might complain this should be in the C# section but i wrote it in C++ and also there seems to be more support going on in the C++ section.

    I have written some code to monitor multiple files in different directories, essentially i created a seperate thread for each directory and file specified. My question is is this the best approach if i want to monitor multiple files in seperate directories?

    here i create the threads for all the files and directories specified
    Code:
            Thread^ t;
            for(int i = 1; i <= (length-1)/2; i++)
    		{
                // Supply the state information required by the task.  
                // Create a thread to execute the task, and then
                // start the thread.					
                t = gcnew Thread(gcnew ThreadStart(gcnew ThreadWithState(args[j],args[j+1]), &ThreadWithState::ThreadProc));
                t->Start();
    			j+=2;
    		}
    		Thread::Sleep(0);
            Console::WriteLine("Main thread does some work, then waits.");
            t->Join();
    and here is the function invoked:
    Code:
        void ThreadProc()
       {
           ... ...
          ........
    
            watcher->NotifyFilter = static_cast<NotifyFilters>(NotifyFilters::LastAccess |
    			NotifyFilters::LastWrite | NotifyFilters::FileName );
    		watcher->Filter = getFileName(); 
    		/*map the events to the corresponding handler / call back function*/
            watcher->Changed += gcnew FileSystemEventHandler( ThreadWithState::fileChanged );
    		watcher->Deleted += gcnew FileSystemEventHandler( ThreadWithState::fileDeleted );
    		watcher->Created += gcnew FileSystemEventHandler( ThreadWithState::fileCreated );
    	    watcher->Renamed += gcnew RenamedEventHandler( ThreadWithState::fileRenamed );	
            watcher->EnableRaisingEvents = true;
    
    		while ( Console::Read() != 'q' );
    		watcher->EnableRaisingEvents = false;
    
       }

    essentially it is creating a Filesystemwatcher per file monitored in the directory.
    If i wanted to monitor hundreds of files all in seperate directoreis will this be efficient enough?

    br

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Why do you keep starting new threads on the same subject?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195
    Quote Originally Posted by Salem View Post
    Why do you keep starting new threads on the same subject?
    Because each one is in a different directory, lol.

  4. #4
    Registered User
    Join Date
    Jul 2010
    Posts
    32
    ok ok i understand the joke. The thread is similar but not the same question as the previous thread. Do you think this is the best approach to go by with what i want?

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Why do you think something that is not C++ should go into the C++ section?
    Let me repeat: THIS IS NOT C++.
    Thus, C++ programmers CANNOT HELP YOU.
    Stop posting threads all with the same subject and leave them in the tech board or C# section. if someone with actual knowledge of the language you're using comes along, you can be certain they'll check THAT section of the board, as well.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Jul 2010
    Posts
    32
    Are you sure? It looks like C++ to me....

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Google the gcnew keyword. It's not C++.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Quote Originally Posted by ashaikh432 View Post
    If i wanted to monitor hundreds of files all in seperate directoreis will this be efficient enough?
    Only you can define 'efficient enough'. Therefore, why don't you try it and tell us if it's efficient enough?

    Basically, you have two options: create a FileSystemWatcher per Filter, or create a FileSystemWatcher that watches C: and ignore events generated by files that you don't care about. After a cursory glance at a Google search (you did do that before asking this question twice on the forums, right?), most people suggested the first way.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. multiple files using filesystemwatcher
    By ashaikh432 in forum C# Programming
    Replies: 2
    Last Post: 08-28-2010, 11:15 AM
  2. thread pool approach
    By fguy817817 in forum C Programming
    Replies: 3
    Last Post: 11-04-2009, 12:59 PM
  3. Approach to File Locking?
    By BigAngryDog in forum Linux Programming
    Replies: 9
    Last Post: 09-19-2009, 02:53 PM
  4. Unsure on how to approach this concurrency problem
    By osiris^ in forum C# Programming
    Replies: 3
    Last Post: 04-29-2008, 11:47 PM
  5. software development approach...
    By dkt in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 10-16-2001, 09:15 PM