What's the best way to check that
-
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 :)
-
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 :)
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
-
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 :)
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
-
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 :)
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 -
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
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 :)
-
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/2001Thanks 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 :)
-
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 :)
Well, if you cannot disable the button, then, I guess, you should go for what Giorgi has suggested
Regards, Lev
-
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 :)
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 -
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 :)
Well, the behavior of this button is rather confused for user. Sometimes, refactor the design would be better choice. :rolleyes:
:) I Love KongFu~
-
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 :)
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.