Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. configuration file

configuration file

Scheduled Pinned Locked Moved C#
csharpwpfwcfxmlquestion
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MarioMARTIN
    wrote on last edited by
    #1

    Hi all, I have a project that consist of several applications (Windows Services and WPF applications communicating via WCF). All applications need certain configuration information, that should be stored in a central / common file. One application should also be able to write to this config file and the other apps should automatically notice these changes and read in the new values. Now I'm looking for a way to realize that. I tried a FileSystemWatcher on a "normal" xml file (not a .Net configuation file) in connection with a Mutex to ensure that no application tries to read the file while another application is writing. It didn't work (The Mutex seemed to be useless, I always got an exception that told me that the file could not be opened because it is already opened by another process). Any ideas how I could implement this stuff? Should I stay by my "normal" xml file or should I switch to an .Net configuration based approach? thanks and regards, MMartin

    C 1 Reply Last reply
    0
    • M MarioMARTIN

      Hi all, I have a project that consist of several applications (Windows Services and WPF applications communicating via WCF). All applications need certain configuration information, that should be stored in a central / common file. One application should also be able to write to this config file and the other apps should automatically notice these changes and read in the new values. Now I'm looking for a way to realize that. I tried a FileSystemWatcher on a "normal" xml file (not a .Net configuation file) in connection with a Mutex to ensure that no application tries to read the file while another application is writing. It didn't work (The Mutex seemed to be useless, I always got an exception that told me that the file could not be opened because it is already opened by another process). Any ideas how I could implement this stuff? Should I stay by my "normal" xml file or should I switch to an .Net configuration based approach? thanks and regards, MMartin

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      The issue you have is that only one application can have the file open, at a time. So, the file watcher isn't going to work. You'd need instead to do something like have a timer which checks the last update time of a file, and have the code written in such a way that it anticipates the possibility of more than one program trying to read the file at once.

      Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

      M 3 Replies Last reply
      0
      • C Christian Graus

        The issue you have is that only one application can have the file open, at a time. So, the file watcher isn't going to work. You'd need instead to do something like have a timer which checks the last update time of a file, and have the code written in such a way that it anticipates the possibility of more than one program trying to read the file at once.

        Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

        M Offline
        M Offline
        MarioMARTIN
        wrote on last edited by
        #3

        Hi Christian, thanks for your reply! "The issue you have is that only one application can have the file open, at a time." That's what I thought, too, and therefore I added a Mutex. Loading the config file: Core.Tracer.Info( "LoadConfigFile: Waiting for Mutex: " + i.ToString() ); m_Mutex.WaitOne(); Core.Tracer.Info( "LoadConfigFile: Locking Mutex: " + i.ToString() ); m_Document.Load( ConfigFileName ); m_Mutex.ReleaseMutex(); Core.Tracer.Info( "LoadConfigFile: Releasing Mutex: " + i.ToString() ); Writing the config file: Core.Tracer.Info( "SaveConfigFile: Waiting for Mutex: " + i.ToString() ); m_Mutex.WaitOne(); Core.Tracer.Info( "SaveConfigFile: Locking Mutex: " + i.ToString() ); if(m_Watcher != null) m_Watcher.EnableRaisingEvents = false; //disable FileSystemWatcher m_Document.Save( ConfigFileName ); if ( m_Watcher != null ) m_Watcher.EnableRaisingEvents = true; m_Mutex.ReleaseMutex(); Core.Tracer.Info( "SaveConfigFile: Releasing Mutex: " + i.ToString() ); But the Mutex is not working. In my log files I see something like this: Application 1 Log: 20.08.2007 07:43:20.730; Agent:; LoadConfigFile: Waiting for Mutex: 1 20.08.2007 07:43:20.730; Agent:; LoadConfigFile: Locking Mutex: 1 20.08.2007 07:43:20.733; Agent:; something happend in LoadConfigFile: The process cannot access the file 'C:\Map\Map.config' because it is being used by another process. Application 2 Log: 20.08.2007 07:43:20.724; Admin:; SaveConfigFile: Waiting for Mutex: 1 20.08.2007 07:43:20.726; Admin:; SaveConfigFile: Locking Mutex: 1 20.08.2007 07:43:20.752; Admin:; SaveConfigFile: Releasing Mutex: 1 I'm losing my mind? ;-) How can Application 1 lock the Mutex at 07:43:20.730 when it is already locked by application 2 at 07:43:20.726? Could it be that Mutex doesn't work in Windows Services? Thanks, MMartin

        1 Reply Last reply
        0
        • C Christian Graus

          The issue you have is that only one application can have the file open, at a time. So, the file watcher isn't going to work. You'd need instead to do something like have a timer which checks the last update time of a file, and have the code written in such a way that it anticipates the possibility of more than one program trying to read the file at once.

          Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

          M Offline
          M Offline
          MarioMARTIN
          wrote on last edited by
          #4

          Aha, it seems that the Mutex is not unique: //create a mutex to assure that only one process at a time //accesses the config file bool isNew = false; m_Mutex = new Mutex( false, "Map_ConfigFile_Mutex", out isNew ); if ( isNew ) { Core.Tracer.Info( "Mutex is new in System" ); } else { Core.Tracer.Info( "Mutex is NOT new in System" ); } Application 1 (Windows Service) log: 20.08.2007 08:09:40.672; Agent:; Mutex is new in System Application 2 (WPF application) log: 20.08.2007 08:09:33.775; Admin:; Mutex is new in System I ensured that it is not a life-time problem. The Mutex object exists as long as the application (which created the Mutex) exists. So: How can a Mutex be created by two different processes. Does it make a difference that one application is a Windows Service? I noticed that, if Application 1 (Windows Service) creates the Mutex, all other Windows Services will say: 20.08.2007 08:09:41.712; Coordinator:; Mutex is NOT new in System very confusing... MMartin

          1 Reply Last reply
          0
          • C Christian Graus

            The issue you have is that only one application can have the file open, at a time. So, the file watcher isn't going to work. You'd need instead to do something like have a timer which checks the last update time of a file, and have the code written in such a way that it anticipates the possibility of more than one program trying to read the file at once.

            Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

            M Offline
            M Offline
            MarioMARTIN
            wrote on last edited by
            #5

            I rephrased my question and asked it again in the C# discussion board, since this entry "went out of scope"... http://www.codeproject.com/script/comments/forums.asp?msg=2189444&forumid=1649&mode=all&userid=4249752#xx2189444xx

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups