Continuous checking processes
-
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.
-
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.
You should move this type of function to a background thread so that it does not block the UI.
-
You should move this type of function to a background thread so that it does not block the UI.
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
-
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
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.
-
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.
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.
-
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.
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.
-
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.
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
-
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
GrooverFromHolland wrote:
I think You don't understand what I am trying to do.
That is true.