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. Windows Forms
  4. How to close specific windows application when Computer is turned off or is restarted .

How to close specific windows application when Computer is turned off or is restarted .

Scheduled Pinned Locked Moved Windows Forms
helpcsharptutorialcode-review
7 Posts 4 Posters 2 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.
  • V Offline
    V Offline
    Vishnu Nath
    wrote on last edited by
    #1

    I have developed a windows application in C#.net that is running in the background like an antivirus. But the problem is whenever i try to switch off my computer or try to restart my computer, the computer doesn't turns off/restarts. And when i individually kills that windows(.exe) application listed out in Processes tab in Windows Task Manager and then tries to switch off the computer or restart it, Everything seems to be fine. How could i enhance or optimize my code so that whenever computer is turned off or is restarted, my application process should be killed automatically. Is there any class in .Net that could possibly find out , when computer is switched off or is restarted, so that i could kill down my application process at that time. Anyone who could help me out in fixing the bug. Thanks in Advance!! :^)

    Aspiring Techie, Vishnu Nath

    A A 2 Replies Last reply
    0
    • V Vishnu Nath

      I have developed a windows application in C#.net that is running in the background like an antivirus. But the problem is whenever i try to switch off my computer or try to restart my computer, the computer doesn't turns off/restarts. And when i individually kills that windows(.exe) application listed out in Processes tab in Windows Task Manager and then tries to switch off the computer or restart it, Everything seems to be fine. How could i enhance or optimize my code so that whenever computer is turned off or is restarted, my application process should be killed automatically. Is there any class in .Net that could possibly find out , when computer is switched off or is restarted, so that i could kill down my application process at that time. Anyone who could help me out in fixing the bug. Thanks in Advance!! :^)

      Aspiring Techie, Vishnu Nath

      A Offline
      A Offline
      Alan N
      wrote on last edited by
      #2

      When Windows wants to shut down it attempts to close all running applications cooperatively and your app must have refused that close request. Have you coded a FormClosing event handler to stop the application closing? e.g.

      private void Form1\_FormClosing(object sender, FormClosingEventArgs e) {
          e.Cancel = true;
      }
      

      The good news is that you can fix it. Take a look at the FormClosingEventArgs.CloseReason Property and code something like this.

      private void Form1\_FormClosing(object sender, FormClosingEventArgs e) {
        if (e.CloseReason == CloseReason.WindowsShutDown) {
          e.Cancel = false;
        } else {
          e.Cancel = true;
        }
      }
      

      Alan.

      modified on Thursday, February 12, 2009 11:17 AM

      V 1 Reply Last reply
      0
      • A Alan N

        When Windows wants to shut down it attempts to close all running applications cooperatively and your app must have refused that close request. Have you coded a FormClosing event handler to stop the application closing? e.g.

        private void Form1\_FormClosing(object sender, FormClosingEventArgs e) {
            e.Cancel = true;
        }
        

        The good news is that you can fix it. Take a look at the FormClosingEventArgs.CloseReason Property and code something like this.

        private void Form1\_FormClosing(object sender, FormClosingEventArgs e) {
          if (e.CloseReason == CloseReason.WindowsShutDown) {
            e.Cancel = false;
          } else {
            e.Cancel = true;
          }
        }
        

        Alan.

        modified on Thursday, February 12, 2009 11:17 AM

        V Offline
        V Offline
        Vishnu Nath
        wrote on last edited by
        #3

        You are right Alan. I have coded the same way as u have mentioned, to keep my apps running in the background. Now i will change the code to your mentioned code. Thanks for the solution. Anyways is there any method where i can start my application and put the notification icon in system tray silently in the background, whenever computer is switched on or is restarted. It is similar to Gtalk or anti- virus starting their application whenever computer is turned on or is restarted. And also Alan, i want my application to behave like system process, so that no one could possibly kill that process from windows task manager processes list, except when computer is switched off. I have seen that you can cannot kill anti-virus processes from windows task manager. How do one convert their application into system processes ensuring that the application could not be killed by administrator of computer(except when comp is switched off). Looking for a solution to this Alan! :doh:

        Aspiring Techie, Vishnu Nath

        L 1 Reply Last reply
        0
        • V Vishnu Nath

          You are right Alan. I have coded the same way as u have mentioned, to keep my apps running in the background. Now i will change the code to your mentioned code. Thanks for the solution. Anyways is there any method where i can start my application and put the notification icon in system tray silently in the background, whenever computer is switched on or is restarted. It is similar to Gtalk or anti- virus starting their application whenever computer is turned on or is restarted. And also Alan, i want my application to behave like system process, so that no one could possibly kill that process from windows task manager processes list, except when computer is switched off. I have seen that you can cannot kill anti-virus processes from windows task manager. How do one convert their application into system processes ensuring that the application could not be killed by administrator of computer(except when comp is switched off). Looking for a solution to this Alan! :doh:

          Aspiring Techie, Vishnu Nath

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Vishnu Nath wrote:

          I have seen that you can cannot kill anti-virus processes from windows task manager.

          You mean that you don't know how to kill that antivirus proces - that doesn't mean that it's not possible. I'm killing the Norton-scanner prior to each build, since Visual Studio requires the resources.

          Vishnu Nath wrote:

          could not be killed by administrator of computer

          I'd consider that a ransom-situation. The administrator owns the damn machine, and should have some control over what it does. Such programs are "malware" and Norton (or Avast, or MacAfee if they still exist) will try to remove it. There's a huge difference in limiting the enduser and in limiting the administrator. Be carefull not to mix up both user-types :-\

          I are troll :)

          V 1 Reply Last reply
          0
          • L Lost User

            Vishnu Nath wrote:

            I have seen that you can cannot kill anti-virus processes from windows task manager.

            You mean that you don't know how to kill that antivirus proces - that doesn't mean that it's not possible. I'm killing the Norton-scanner prior to each build, since Visual Studio requires the resources.

            Vishnu Nath wrote:

            could not be killed by administrator of computer

            I'd consider that a ransom-situation. The administrator owns the damn machine, and should have some control over what it does. Such programs are "malware" and Norton (or Avast, or MacAfee if they still exist) will try to remove it. There's a huge difference in limiting the enduser and in limiting the administrator. Be carefull not to mix up both user-types :-\

            I are troll :)

            V Offline
            V Offline
            Vishnu Nath
            wrote on last edited by
            #5

            You are right. Thanks for your valuable advice and an important guidance to my project. Thanks a Lot!! :-D

            Aspiring Techie, Vishnu Nath

            1 Reply Last reply
            0
            • V Vishnu Nath

              I have developed a windows application in C#.net that is running in the background like an antivirus. But the problem is whenever i try to switch off my computer or try to restart my computer, the computer doesn't turns off/restarts. And when i individually kills that windows(.exe) application listed out in Processes tab in Windows Task Manager and then tries to switch off the computer or restart it, Everything seems to be fine. How could i enhance or optimize my code so that whenever computer is turned off or is restarted, my application process should be killed automatically. Is there any class in .Net that could possibly find out , when computer is switched off or is restarted, so that i could kill down my application process at that time. Anyone who could help me out in fixing the bug. Thanks in Advance!! :^)

              Aspiring Techie, Vishnu Nath

              A Offline
              A Offline
              ashok_rgm
              wrote on last edited by
              #6

              Hi, I think here you can run a small windows service related to your application, but am not sure about it. or can try IPC concept. regards,

              ashok

              V 1 Reply Last reply
              0
              • A ashok_rgm

                Hi, I think here you can run a small windows service related to your application, but am not sure about it. or can try IPC concept. regards,

                ashok

                V Offline
                V Offline
                Vishnu Nath
                wrote on last edited by
                #7

                Alan has given out me correct solution to my problem. And now i dont face any problem in shutting down my PC after rectifying my code. ;)

                Aspiring Techie, Vishnu Nath

                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