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. Threading

Threading

Scheduled Pinned Locked Moved C#
csharptutorial
3 Posts 3 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.
  • R Offline
    R Offline
    rakesh_nits
    wrote on last edited by
    #1

    I am using file watcher and a timer in a windows service and i want that timer_elapsed function should be called only when any file sytem watcher event is not working.i think it can be done using threading if i will assign lowest priority to the timer_elapsed thread.but i am not able to do it. can anybody can tell me how to do it in C#.

    G A 2 Replies Last reply
    0
    • R rakesh_nits

      I am using file watcher and a timer in a windows service and i want that timer_elapsed function should be called only when any file sytem watcher event is not working.i think it can be done using threading if i will assign lowest priority to the timer_elapsed thread.but i am not able to do it. can anybody can tell me how to do it in C#.

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      I would use something as simple as a static variable as a counter. Increase the counter when a watcher event starts, and decrese it when it ends. Check the counter in the timer, and only do anything if the counter is zero. --- b { font-weight: normal; }

      1 Reply Last reply
      0
      • R rakesh_nits

        I am using file watcher and a timer in a windows service and i want that timer_elapsed function should be called only when any file sytem watcher event is not working.i think it can be done using threading if i will assign lowest priority to the timer_elapsed thread.but i am not able to do it. can anybody can tell me how to do it in C#.

        A Offline
        A Offline
        Andy Brummer
        wrote on last edited by
        #3

        In C# the easiest way to accomplish this is to use a lock on an object.

         private static object sLock = new object();
        
         private void Timer\_elapsed(object source, EventArgs e)
         {
            lock (sLock)
            {
               //Timer code here.
            }
         }
        
         private void FileWatcher\_event(object source, EventArgs e)
         {
            lock (sLock)
            {
               //File code here.
            }
         }
        

        any code in the lock blocks is protected from multiple threads accessing it at the same time. I do something similar with the timer that I wrote: http://www.codeproject.com/dotnet/ABTransClockArticle.asp[^]


        I can imagine the sinking feeling one would have after ordering my book, only to find a laughably ridiculous theory with demented logic once the book arrives - Mark McCutcheon

        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