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. Continuous checking processes

Continuous checking processes

Scheduled Pinned Locked Moved C#
comdesignquestion
8 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.
  • G Offline
    G Offline
    GrooverFromHolland
    wrote on last edited by
    #1

    Hi all, I am using a windows form timer to check running processes, but this is not a good way to do it, it is blocking the UI. Wat would be the best way to do this? This is what I use:

    private void check_RunningProcessTimer_Tick(object sender, EventArgs e) //interval 100.
    {
    // needed because main program is running in "kiosk mode".
    if (IsProcessRunning("Some_proces"))
    {
    this.SendToBack();
    }
    else
    {
    this.Activate();
    }
    // Needed to make On screen keyboard button a toggle button.
    if (IsProcessRunning("osk"))
    {
    btnOskKeyBoard.Text ="X" ;
    }
    else
    {
    btnOskKeyBoard.Text = "";
    }

        }
    

    I would also like to do this for other things like checking if Com port is open or closed. Thanks, Groover.

    L 1 Reply Last reply
    0
    • G GrooverFromHolland

      Hi all, I am using a windows form timer to check running processes, but this is not a good way to do it, it is blocking the UI. Wat would be the best way to do this? This is what I use:

      private void check_RunningProcessTimer_Tick(object sender, EventArgs e) //interval 100.
      {
      // needed because main program is running in "kiosk mode".
      if (IsProcessRunning("Some_proces"))
      {
      this.SendToBack();
      }
      else
      {
      this.Activate();
      }
      // Needed to make On screen keyboard button a toggle button.
      if (IsProcessRunning("osk"))
      {
      btnOskKeyBoard.Text ="X" ;
      }
      else
      {
      btnOskKeyBoard.Text = "";
      }

          }
      

      I would also like to do this for other things like checking if Com port is open or closed. Thanks, Groover.

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

      You should move this type of function to a background thread so that it does not block the UI.

      speaking as ...

      G 1 Reply Last reply
      0
      • L Lost User

        You should move this type of function to a background thread so that it does not block the UI.

        speaking as ...

        G Offline
        G Offline
        GrooverFromHolland
        wrote on last edited by
        #3

        I have never used threads before and I do not really know how to implement it, please help me on the way. some questions: Should I include the public bool that returns the running process in the thread? Do I have to create a event in the thread for each process change? How do I deal with with change of Com port change? I think of using a system timer with autoreset, what would be the timer interval to make sure all the changes are detected? this is the public bool I use ;

        public bool IsProcessRunning(string name)
        {
        foreach (Process clsProcess in Process.GetProcesses())
        {
        if (clsProcess.ProcessName.Contains(name))
        {
        return true;
        }
        }

                return false;
            }
        

        Please point me in the right direction, thanks, Groover

        L 1 Reply Last reply
        0
        • G GrooverFromHolland

          I have never used threads before and I do not really know how to implement it, please help me on the way. some questions: Should I include the public bool that returns the running process in the thread? Do I have to create a event in the thread for each process change? How do I deal with with change of Com port change? I think of using a system timer with autoreset, what would be the timer interval to make sure all the changes are detected? this is the public bool I use ;

          public bool IsProcessRunning(string name)
          {
          foreach (Process clsProcess in Process.GetProcesses())
          {
          if (clsProcess.ProcessName.Contains(name))
          {
          return true;
          }
          }

                  return false;
              }
          

          Please point me in the right direction, thanks, Groover

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

          I don't think those questions can be reasonably answered in a forum like this. I would suggest you try a search for some of the articles here that deal with thread programming, and try a few test programs to get familiar with the issues surrounding synchronisation and data transfer between processes.

          speaking as ...

          G 1 Reply Last reply
          0
          • L Lost User

            I don't think those questions can be reasonably answered in a forum like this. I would suggest you try a search for some of the articles here that deal with thread programming, and try a few test programs to get familiar with the issues surrounding synchronisation and data transfer between processes.

            speaking as ...

            G Offline
            G Offline
            GrooverFromHolland
            wrote on last edited by
            #5

            OK, I changed Windows forms timer to System.Threading.Timer and Invoked all UI controls in the TimerCallback. There is one problem left, the:

            this.Activate()

            is called every timer timeout and this causes a problem with dialogs and message boxes. I can do a:

            runningProcess_Timer.Change(Timeout.Infinite, Timeout.Infinite); //disable timer//

            for every call to a dialog or message and a:

            runningProcess_Timer.Change(0, 100); //enable timer

            after closing. I there an event I can create or handle on dialogs and message boxes I can use to disable and enable the timer when these pop-ups need attention? Groover.

            L 1 Reply Last reply
            0
            • G GrooverFromHolland

              OK, I changed Windows forms timer to System.Threading.Timer and Invoked all UI controls in the TimerCallback. There is one problem left, the:

              this.Activate()

              is called every timer timeout and this causes a problem with dialogs and message boxes. I can do a:

              runningProcess_Timer.Change(Timeout.Infinite, Timeout.Infinite); //disable timer//

              for every call to a dialog or message and a:

              runningProcess_Timer.Change(0, 100); //enable timer

              after closing. I there an event I can create or handle on dialogs and message boxes I can use to disable and enable the timer when these pop-ups need attention? Groover.

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

              You are not doing it quite right. All the UI code including dialogs and message boxes (which are merely custom dialogs) should run in the main thread without interference or timers. Anything that requires periods of inactivity (such as sleep, wait for external event etc) should run in a background thread. The background threads can communicate with the foreground by using shared memory or sending messages. I'm sure you can find some good samples in the Articles section.

              speaking as ...

              G 1 Reply Last reply
              0
              • L Lost User

                You are not doing it quite right. All the UI code including dialogs and message boxes (which are merely custom dialogs) should run in the main thread without interference or timers. Anything that requires periods of inactivity (such as sleep, wait for external event etc) should run in a background thread. The background threads can communicate with the foreground by using shared memory or sending messages. I'm sure you can find some good samples in the Articles section.

                speaking as ...

                G Offline
                G Offline
                GrooverFromHolland
                wrote on last edited by
                #7

                Thank You for Your advice, I think You don't understand what I am trying to do. On my main form I have a button that starts the on screen keyboard and if it is running kills it. There is no problem unless the user uses the close box on the application. To make the button the right state(close or open) I use the timer to check if the application is running or not. The same with the Some_process, it has to be topmost until it closes. I have no code for Some_process, it is not my application. Some_process is killed by a comport event and my code makes my application topmost again and hide the task-bar and start menu. If the user closes Some_process with the closing button, my application looses focus. To prevent this I have to check if Some_process is running or not. I have changed the code in my timer handling and it seems not to interfere with any forms an custom message boxes. Not sure if it interferes with message boxes from System.Windows.Forms MessageBox Class and dialogs. This is what I have now:

                System.Threading.Timer runningProcess_Timer;

                	runningProcess\_Timer = new System.Threading.Timer(new TimerCallback(checkRunningProcesses), null, 0, 100);		
                	
                	  private void checkRunningProcesses(object obj)
                    {
                        if (this.IsHandleCreated)
                        {
                            if (!IsProcessRunning("osk"))
                            {
                                this.Invoke(new EventHandler(delegate
                                {
                                    btnKeyBoard.Text = "";
                                }));
                            }
                            else
                            {
                                this.Invoke(new EventHandler(delegate
                                {
                                    btnKeyBoard.Text = "X";
                                }));
                            }
                
                            if (!IsProcessRunning("Some\_process"))
                            {
                                this.Invoke(new EventHandler(delegate
                                {
                                    int i = 0;
                                    foreach (Form f in Application.OpenForms)
                                    {
                                        if (f.Enabled)
                                        {
                                            i += 1;
                                        }
                                    }
                
                                    if (i < 2)
                                    {
                                        this.Activate();
                                    }
                                }));
                            }
                        }
                    }
                

                Groover

                L 1 Reply Last reply
                0
                • G GrooverFromHolland

                  Thank You for Your advice, I think You don't understand what I am trying to do. On my main form I have a button that starts the on screen keyboard and if it is running kills it. There is no problem unless the user uses the close box on the application. To make the button the right state(close or open) I use the timer to check if the application is running or not. The same with the Some_process, it has to be topmost until it closes. I have no code for Some_process, it is not my application. Some_process is killed by a comport event and my code makes my application topmost again and hide the task-bar and start menu. If the user closes Some_process with the closing button, my application looses focus. To prevent this I have to check if Some_process is running or not. I have changed the code in my timer handling and it seems not to interfere with any forms an custom message boxes. Not sure if it interferes with message boxes from System.Windows.Forms MessageBox Class and dialogs. This is what I have now:

                  System.Threading.Timer runningProcess_Timer;

                  	runningProcess\_Timer = new System.Threading.Timer(new TimerCallback(checkRunningProcesses), null, 0, 100);		
                  	
                  	  private void checkRunningProcesses(object obj)
                      {
                          if (this.IsHandleCreated)
                          {
                              if (!IsProcessRunning("osk"))
                              {
                                  this.Invoke(new EventHandler(delegate
                                  {
                                      btnKeyBoard.Text = "";
                                  }));
                              }
                              else
                              {
                                  this.Invoke(new EventHandler(delegate
                                  {
                                      btnKeyBoard.Text = "X";
                                  }));
                              }
                  
                              if (!IsProcessRunning("Some\_process"))
                              {
                                  this.Invoke(new EventHandler(delegate
                                  {
                                      int i = 0;
                                      foreach (Form f in Application.OpenForms)
                                      {
                                          if (f.Enabled)
                                          {
                                              i += 1;
                                          }
                                      }
                  
                                      if (i < 2)
                                      {
                                          this.Activate();
                                      }
                                  }));
                              }
                          }
                      }
                  

                  Groover

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

                  GrooverFromHolland wrote:

                  I think You don't understand what I am trying to do.

                  That is true.

                  speaking as ...

                  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