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. Question abt Service

Question abt Service

Scheduled Pinned Locked Moved C#
questionhelp
8 Posts 4 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.
  • S Offline
    S Offline
    SatyaDY
    wrote on last edited by
    #1

    Hi, I am trying find the services which are running on my machine on polling basis, and I am trying write this to a file before shutdown. I am trying to do this using a service, I am writing the code for file writing in the OnStop() method of the service. The problem is my service starts and stops immediately. Why this is happening?? Code Snippet: OnStop() { System.IO.StreamWriter file = new System.IO.StreamWriter("d:\\test.txt"); Process [] curentProcess= Process.GetProcesses(); foreach(Process p in curentProcess) file.WriteLine(p.ProcessName); file.Close(); } Regards, Satya

    C N 2 Replies Last reply
    0
    • S SatyaDY

      Hi, I am trying find the services which are running on my machine on polling basis, and I am trying write this to a file before shutdown. I am trying to do this using a service, I am writing the code for file writing in the OnStop() method of the service. The problem is my service starts and stops immediately. Why this is happening?? Code Snippet: OnStop() { System.IO.StreamWriter file = new System.IO.StreamWriter("d:\\test.txt"); Process [] curentProcess= Process.GetProcesses(); foreach(Process p in curentProcess) file.WriteLine(p.ProcessName); file.Close(); } Regards, Satya

      C Offline
      C Offline
      Corinna John
      wrote on last edited by
      #2

      SatyaDY wrote: my service starts and stops immediately Your service does not even reach OnStop(), the problem is Main() or in OnStart(). To locate the failure, check the event log. Are there any exception messages from your service? If not, you can write a log file during OnStart(), to see where the service stops.

      S 1 Reply Last reply
      0
      • S SatyaDY

        Hi, I am trying find the services which are running on my machine on polling basis, and I am trying write this to a file before shutdown. I am trying to do this using a service, I am writing the code for file writing in the OnStop() method of the service. The problem is my service starts and stops immediately. Why this is happening?? Code Snippet: OnStop() { System.IO.StreamWriter file = new System.IO.StreamWriter("d:\\test.txt"); Process [] curentProcess= Process.GetProcesses(); foreach(Process p in curentProcess) file.WriteLine(p.ProcessName); file.Close(); } Regards, Satya

        N Offline
        N Offline
        Nick Seng
        wrote on last edited by
        #3

        ummm... I don't really have a solution to your problem, but I think I see a problem with your service. Basically, in order for your service to work, it has to be the first service to stopped during shutdown, else the GetProcess Method won't return those processes stopped before hand.


        "if you vote me down, I shall become more powerful than you can possibly imagine" - Michael P. Butler.

        S 1 Reply Last reply
        0
        • C Corinna John

          SatyaDY wrote: my service starts and stops immediately Your service does not even reach OnStop(), the problem is Main() or in OnStart(). To locate the failure, check the event log. Are there any exception messages from your service? If not, you can write a log file during OnStart(), to see where the service stops.

          S Offline
          S Offline
          SatyaDY
          wrote on last edited by
          #4

          check the event log Yeah, I checked the event log, it's having entries for start and stop. When I comment the code, for file opening and writing, I find it running without any problem. How the file writing of the process names giving the problem I couldn't get. Regards, Satya

          1 Reply Last reply
          0
          • N Nick Seng

            ummm... I don't really have a solution to your problem, but I think I see a problem with your service. Basically, in order for your service to work, it has to be the first service to stopped during shutdown, else the GetProcess Method won't return those processes stopped before hand.


            "if you vote me down, I shall become more powerful than you can possibly imagine" - Michael P. Butler.

            S Offline
            S Offline
            SatyaDY
            wrote on last edited by
            #5

            I think you are correct. Because the GetProcess couldn't get the Processes, file writing is also failing and the service is stopping immediately. Can we give priority (for making it first to stop)?? Regards, Satya

            N 1 Reply Last reply
            0
            • S SatyaDY

              I think you are correct. Because the GetProcess couldn't get the Processes, file writing is also failing and the service is stopping immediately. Can we give priority (for making it first to stop)?? Regards, Satya

              N Offline
              N Offline
              Nick Seng
              wrote on last edited by
              #6

              I'm not too familiar with services, but you might try and see doing this

              		Thread th = Thread.CurrentThread;
              		th.Priority = ThreadPriority.Highest;
              

              during onStart if it will help. :~


              "if you vote me down, I shall become more powerful than you can possibly imagine" - Michael P. Butler.

              D 1 Reply Last reply
              0
              • N Nick Seng

                I'm not too familiar with services, but you might try and see doing this

                		Thread th = Thread.CurrentThread;
                		th.Priority = ThreadPriority.Highest;
                

                during onStart if it will help. :~


                "if you vote me down, I shall become more powerful than you can possibly imagine" - Michael P. Butler.

                D Offline
                D Offline
                Dave Kreskowiak
                wrote on last edited by
                #7

                Whoa there Tex! Really bad idea. This will not give priority to the service to be shutdown first. As a matter of fact, you'll just end up slowing down the rest of the system doing this... You can't give priority to any service to shutdown first. You might want to try overriding WndProc and watching for the WM_QUERYENDSESSION message. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

                N 1 Reply Last reply
                0
                • D Dave Kreskowiak

                  Whoa there Tex! Really bad idea. This will not give priority to the service to be shutdown first. As a matter of fact, you'll just end up slowing down the rest of the system doing this... You can't give priority to any service to shutdown first. You might want to try overriding WndProc and watching for the WM_QUERYENDSESSION message. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

                  N Offline
                  N Offline
                  Nick Seng
                  wrote on last edited by
                  #8

                  Well, so much for that idea. :)


                  "if you vote me down, I shall become more powerful than you can possibly imagine" - Michael P. Butler.

                  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