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. What's the best way to check that

What's the best way to check that

Scheduled Pinned Locked Moved C#
helpquestion
10 Posts 6 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.
  • C Offline
    C Offline
    CodingLover
    wrote on last edited by
    #1

    Hi all, I want to execute and exe, basically a process. Everything is fine. But I want to know what's the best way to handle this. Let me explain. Say I start the process on a click event, but the button is not disable. So a user can click on it again. At that point I want to check that process is already running. If it's running I don't want to run it again. What I have done here is, use a global variable and once the process is started make it to true. Once a user click that button, simply validate that variable. Is that a good practice? Thanks

    I appreciate your help all the time... CodingLover :)

    L G realJSOPR S 4 Replies Last reply
    0
    • C CodingLover

      Hi all, I want to execute and exe, basically a process. Everything is fine. But I want to know what's the best way to handle this. Let me explain. Say I start the process on a click event, but the button is not disable. So a user can click on it again. At that point I want to check that process is already running. If it's running I don't want to run it again. What I have done here is, use a global variable and once the process is started make it to true. Once a user click that button, simply validate that variable. Is that a good practice? Thanks

      I appreciate your help all the time... CodingLover :)

      L Offline
      L Offline
      Lev Danielyan
      wrote on last edited by
      #2

      I would disable the button on starting the process and enable it back when the process is done working. Like this:

      private void button1_Click_1(object sender, EventArgs e) {
      button1.Enabled = false;
      Process proc = new Process();
      proc.StartInfo.FileName = "notepad.exe";
      proc.EnableRaisingEvents = true;
      proc.Exited += new EventHandler(proc_Exited);

      proc.Start();
      }

      void proc_Exited(object sender, EventArgs e) {
      button1.Enabled = true;
      }

      Regards, Lev

      C 1 Reply Last reply
      0
      • C CodingLover

        Hi all, I want to execute and exe, basically a process. Everything is fine. But I want to know what's the best way to handle this. Let me explain. Say I start the process on a click event, but the button is not disable. So a user can click on it again. At that point I want to check that process is already running. If it's running I don't want to run it again. What I have done here is, use a global variable and once the process is started make it to true. Once a user click that button, simply validate that variable. Is that a good practice? Thanks

        I appreciate your help all the time... CodingLover :)

        G Offline
        G Offline
        Giorgi Dalakishvili
        wrote on last edited by
        #3

        You can use Process.GetProcessesByName[^] method to find out if the process is already running.

        Giorgi Dalakishvili #region signature My Articles Asynchronous Registry Notification Using Strongly-typed WMI Classes in .NET [^] My blog #endregion

        1 Reply Last reply
        0
        • C CodingLover

          Hi all, I want to execute and exe, basically a process. Everything is fine. But I want to know what's the best way to handle this. Let me explain. Say I start the process on a click event, but the button is not disable. So a user can click on it again. At that point I want to check that process is already running. If it's running I don't want to run it again. What I have done here is, use a global variable and once the process is started make it to true. Once a user click that button, simply validate that variable. Is that a good practice? Thanks

          I appreciate your help all the time... CodingLover :)

          realJSOPR Offline
          realJSOPR Offline
          realJSOP
          wrote on last edited by
          #4

          I have a class that I call Runner. It starts the required process, and then starts a background worker object that serves to allow the programmer to terminate the process. When the process terminates (either normally or by user-intervention, it fires a custom event and the calling form handles the event. Using a class like this, you could disable the button when the process is started, and re-enable it when the process terminates. Alternatively, you could leave the button enabled and check to see if a globally-defined runner object is != null to see if you should display a message to the user or not. I originally wrote this code so that I could track how much time was spent running the spawned process.

          "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
          -----
          "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

          C 1 Reply Last reply
          0
          • L Lev Danielyan

            I would disable the button on starting the process and enable it back when the process is done working. Like this:

            private void button1_Click_1(object sender, EventArgs e) {
            button1.Enabled = false;
            Process proc = new Process();
            proc.StartInfo.FileName = "notepad.exe";
            proc.EnableRaisingEvents = true;
            proc.Exited += new EventHandler(proc_Exited);

            proc.Start();
            }

            void proc_Exited(object sender, EventArgs e) {
            button1.Enabled = true;
            }

            Regards, Lev

            C Offline
            C Offline
            CodingLover
            wrote on last edited by
            #5

            But the reason is I cannot disable the button. On that single click event I want to start two process. One of it is continuously running, that's what I want to check in this case. Other process can be start/stop randomly.

            I appreciate your help all the time... CodingLover :)

            L D 2 Replies Last reply
            0
            • realJSOPR realJSOP

              I have a class that I call Runner. It starts the required process, and then starts a background worker object that serves to allow the programmer to terminate the process. When the process terminates (either normally or by user-intervention, it fires a custom event and the calling form handles the event. Using a class like this, you could disable the button when the process is started, and re-enable it when the process terminates. Alternatively, you could leave the button enabled and check to see if a globally-defined runner object is != null to see if you should display a message to the user or not. I originally wrote this code so that I could track how much time was spent running the spawned process.

              "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
              -----
              "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

              C Offline
              C Offline
              CodingLover
              wrote on last edited by
              #6

              Thanks for the comment. Ya, that's the way I'm normally working on. But the thing here is I cannot disable the button. As I said in earlier post, in that click event I have to start two process.

              I appreciate your help all the time... CodingLover :)

              realJSOPR 1 Reply Last reply
              0
              • C CodingLover

                But the reason is I cannot disable the button. On that single click event I want to start two process. One of it is continuously running, that's what I want to check in this case. Other process can be start/stop randomly.

                I appreciate your help all the time... CodingLover :)

                L Offline
                L Offline
                Lev Danielyan
                wrote on last edited by
                #7

                Well, if you cannot disable the button, then, I guess, you should go for what Giorgi has suggested

                Regards, Lev

                1 Reply Last reply
                0
                • C CodingLover

                  Thanks for the comment. Ya, that's the way I'm normally working on. But the thing here is I cannot disable the button. As I said in earlier post, in that click event I have to start two process.

                  I appreciate your help all the time... CodingLover :)

                  realJSOPR Offline
                  realJSOPR Offline
                  realJSOP
                  wrote on last edited by
                  #8

                  Would you like me to post the code for you?

                  "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                  -----
                  "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                  1 Reply Last reply
                  0
                  • C CodingLover

                    But the reason is I cannot disable the button. On that single click event I want to start two process. One of it is continuously running, that's what I want to check in this case. Other process can be start/stop randomly.

                    I appreciate your help all the time... CodingLover :)

                    D Offline
                    D Offline
                    Dragonfly_Lee
                    wrote on last edited by
                    #9

                    Well, the behavior of this button is rather confused for user. Sometimes, refactor the design would be better choice. :rolleyes:

                    :) I Love KongFu~

                    1 Reply Last reply
                    0
                    • C CodingLover

                      Hi all, I want to execute and exe, basically a process. Everything is fine. But I want to know what's the best way to handle this. Let me explain. Say I start the process on a click event, but the button is not disable. So a user can click on it again. At that point I want to check that process is already running. If it's running I don't want to run it again. What I have done here is, use a global variable and once the process is started make it to true. Once a user click that button, simply validate that variable. Is that a good practice? Thanks

                      I appreciate your help all the time... CodingLover :)

                      S Offline
                      S Offline
                      sourabhsorate
                      wrote on last edited by
                      #10

                      Hello, Here is the code private void button3_Click(object sender, EventArgs e) { button3.Enabled = false; Process proc = new Process(); proc.StartInfo.FileName = "notepad.exe"; proc.EnableRaisingEvents = true; proc.Exited += new EventHandler(proc_Exited); proc.Start(); } void proc_Exited(object sender, EventArgs e) { myDelgate d = new myDelgate(enableBtn); this.Invoke(d); } void enableBtn() { button3.Enabled = true; } Remember u should add the statement public delegate void myDelgate(); inside the class which declare a delegate I hav sucessfully disable the button on starting notepad and enable afte exiting the notepad hope this code will help you.

                      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