Author Topic: Trouble with "ElapsedEventHandler"; How to pause it?  (Read 3974 times)

0 Members and 1 Guest are viewing this topic.

T.Willey

  • Needs a day job
  • Posts: 5247
Trouble with "ElapsedEventHandler"; How to pause it?
« on: September 08, 2006, 01:39:02 PM »
I have this code, which acts as a count down program to end other programs.  It will list all the process that are running.  Then you hit the start button, and the counter starts counting down.  Once it reaches all zeros, it will end the processes highlighted at that time.  I want to be able to stop it, but I can't do that yet.

Any/all help is appreciated.  Thanks in advance.

Code: [Select]
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Diagnostics;
using System.Timers;
using System.Collections;

namespace WindosApps
{
/// <summary>
/// Description of CountDown.
/// </summary>
public class CountDown : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.ListBox listBox1;
public CountDown()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();

//
// TODO: Add constructor code after the InitializeComponent() call.
//
}

[STAThread]
public static void Main(string[] args)
{
Application.Run(new CountDown());
}
#region Windows Forms Designer generated code
/// <summary>
/// This method is required for Windows Forms designer support.
/// Do not change the method contents inside the source code editor. The Forms designer might
/// not be able to load this method if it was changed manually.
/// </summary>
private void InitializeComponent() {
this.listBox1 = new System.Windows.Forms.ListBox();
this.button1 = new System.Windows.Forms.Button();
this.label2 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// listBox1
//
this.listBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)));
this.listBox1.BackColor = System.Drawing.SystemColors.Desktop;
this.listBox1.Font = new System.Drawing.Font("Baskerville Old Face", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.listBox1.ForeColor = System.Drawing.SystemColors.Window;
this.listBox1.ItemHeight = 14;
this.listBox1.Location = new System.Drawing.Point(16, 32);
this.listBox1.Name = "listBox1";
this.listBox1.SelectionMode = System.Windows.Forms.SelectionMode.MultiExtended;
this.listBox1.Size = new System.Drawing.Size(216, 186);
this.listBox1.TabIndex = 0;
//
// button1
//
this.button1.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
this.button1.Location = new System.Drawing.Point(16, 328);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(56, 24);
this.button1.TabIndex = 1;
this.button1.Text = "Start";
this.button1.Click += new System.EventHandler(this.Button1Click);
//
// label2
//
this.label2.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
this.label2.Location = new System.Drawing.Point(8, 232);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(224, 16);
this.label2.TabIndex = 4;
this.label2.Text = "Time remaining.";
//
// textBox1
//
this.textBox1.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
this.textBox1.BackColor = System.Drawing.SystemColors.Info;
this.textBox1.Font = new System.Drawing.Font("CityBlueprint", 26.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(2)));
this.textBox1.ForeColor = System.Drawing.SystemColors.ActiveCaption;
this.textBox1.Location = new System.Drawing.Point(16, 256);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(216, 52);
this.textBox1.TabIndex = 2;
this.textBox1.Text = "00:00:00";
this.textBox1.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
//
// label1
//
this.label1.Anchor = System.Windows.Forms.AnchorStyles.Top;
this.label1.Location = new System.Drawing.Point(8, 8);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(240, 17);
this.label1.TabIndex = 3;
this.label1.Text = "Highlight processes to end when timer expires.";
//
// button2
//
this.button2.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
this.button2.Location = new System.Drawing.Point(176, 328);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(56, 24);
this.button2.TabIndex = 1;
this.button2.Text = "Exit";
this.button2.Click += new System.EventHandler(this.Button2Click);
//
// button3
//
this.button3.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
this.button3.Location = new System.Drawing.Point(96, 328);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(56, 24);
this.button3.TabIndex = 1;
this.button3.Text = "Stop";
this.button3.Click += new System.EventHandler(this.Button3Click);
//
// CountDown
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.BackColor = System.Drawing.SystemColors.ActiveBorder;
this.ClientSize = new System.Drawing.Size(248, 365);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button1);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.button2);
this.Controls.Add(this.button3);
this.MaximumSize = new System.Drawing.Size(256, 700);
this.MinimumSize = new System.Drawing.Size(256, 392);
this.Name = "CountDown";
this.Text = "CountDown";
this.Load += new System.EventHandler(this.CountDownLoad);
this.ResumeLayout(false);
}
#endregion
void Button2Click(object sender, System.EventArgs e)
{
this.Close();
}

void CountDownLoad(object sender, System.EventArgs e)
{
Process[] ProAr = System.Diagnostics.Process.GetProcesses();
string[] StrAr = new string[ProAr.Length];
int cnt = 0;
foreach (Process Pro in ProAr) {
StrAr[cnt] = Pro.ProcessName;
++cnt;
}
Array.Sort (StrAr);
foreach (string Str in StrAr) {
listBox1.Items.Add (Str);
}
}
void Button1Click(object sender, System.EventArgs e)
{
if (this.textBox1.Text.Length != 8) {
MessageBox.Show ("Need to enter time in hours minutes seconds. (00:00:00)");
}
else {
System.Timers.Timer CountDownTimer = new System.Timers.Timer();
CountDownTimer.Elapsed += new ElapsedEventHandler (SecondCount);
CountDownTimer.Interval = 1000;
CountDownTimer.Enabled = true;
}
}
private void SecondCount (object sender, ElapsedEventArgs e) {
this.textBox1.Text = SubtractSecond (this.textBox1.Text);
if (this.textBox1.Text == "00:00:00") {
foreach (string Str in this.listBox1.SelectedItems) {
foreach (Process Pro in System.Diagnostics.Process.GetProcessesByName (Str)) {
Pro.Kill();
}
}
//e.Finalize;
this.Close();
}
}
private string SubtractSecond (string CurTime) {
int Hour = Convert.ToInt32 (CurTime.Substring (0,2));
int Minute = Convert.ToInt16 (CurTime.Substring (3,2));
int Second = Convert.ToInt16 (CurTime.Substring (6,2));
string strHour = "00";
string strMinute = "00";
string strSecond = "00";
if ((Second == 1 && Minute == 0 && Hour == 0) || (Second == 0 && Minute == 0 && Hour == 0)) {
return "00:00:00";
}
if (Second == 0) {
Minute = Minute - 1;
Second = 59;
if (Minute == 0 && Hour > 0) {
Hour = Hour - 1;
Minute = 59;
}
}
else {
Second = Second - 1;
}
if (Minute < 0) {
Minute = 0;
}
if (Hour < 0) {
Hour = 0;
}
if (Second < 10) {
strSecond = "0" + Convert.ToString (Second);
}
else {
strSecond = Convert.ToString (Second);
}
if (Minute < 10) {
strMinute = "0" + Convert.ToString (Minute);
}
else {
strMinute = Convert.ToString (Minute);
}
if (Hour < 10) {
strHour = "0" + Convert.ToString (Hour);
}
else {
strHour = Convert.ToString (Hour);
}
return strHour + ":" + strMinute + ":" + strSecond;
}
void Button3Click(object sender, System.EventArgs e)
{
//CountDown.Button1Click.CountDownTimer.Enabled = false;
}

}
}
« Last Edit: September 08, 2006, 01:51:16 PM by T.Willey »
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Trouble with "ElapsedEventHandler"; How to pause it?
« Reply #1 on: September 08, 2006, 07:11:26 PM »
The .Enabled property can be changed, would that help ?

http://msdn2.microsoft.com/en-us/library/system.timers.timer.enabled.aspx
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate to theSwamp<--

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Trouble with "ElapsedEventHandler"; How to pause it?
« Reply #2 on: September 08, 2006, 07:13:17 PM »
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate to theSwamp<--

T.Willey

  • Needs a day job
  • Posts: 5247
Re: Trouble with "ElapsedEventHandler"; How to pause it?
« Reply #3 on: September 08, 2006, 07:17:23 PM »
The .Enabled property can be changed, would that help ?

http://msdn2.microsoft.com/en-us/library/system.timers.timer.enabled.aspx
It would if I could get to it from another sub, I haven't been able to in my testing.

.. and there is the Stop Method ...
http://msdn2.microsoft.com/en-us/library/system.timers.timer.stop.aspx
I didn't know about the stop, I will see if I can get to it.

Thanks Kerry.


On a side note.  I was going to post another question before I saw the responses by Kerry.
The program doesn't seem to get all the process associated with a program.  I tested it with winamp, and it worked fine, but when I tried it with the built-in cd player from windows, it didn't close it completely.  How can I get a list of all processes associated with a program? Or how can I only list programs instead of processes?  I will try looking at these two questions on Monday more, as I don't I have time over the weekend.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

T.Willey

  • Needs a day job
  • Posts: 5247
Re: Trouble with "ElapsedEventHandler"; How to pause it?
« Reply #4 on: September 08, 2006, 07:31:16 PM »
Found one way to close the cd player program.
Code: [Select]
foreach (Process Pro in System.Diagnostics.Process.GetProcessesByName (Str)) {
if (!Pro.CloseMainWindow()) {
Pro.Kill();
}
}
From msdn
Quote
      CloseMainWindow      Closes a process that has a user interface by sending a close message to its main window. 
I figure if it can't be close by closing the main window, then the kill method will work.  Will keep looking.

Now I'm looking for a way to tell if a process is a system process (ones that you don't want to/can't close) so that I can exclude them from the list.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

T.Willey

  • Needs a day job
  • Posts: 5247
Re: Trouble with "ElapsedEventHandler"; How to pause it?
« Reply #5 on: September 11, 2006, 03:27:31 PM »
To get this to work, I added my calling definition of the timer object to the main class of the code (which is how the MSDN site says to assign it).  Now I can call that object from any portion of the code, which is how I want it.

Now my problem is when I hit the stop button, it stops, but when I hit the start button again, it will decrease the time by the amount of times I have hit the start button, ie: I hit it once, it will decrease by one, when I stop it and start it again (twice now) it will decrease by two, if I do it again (three times now) it will decrease by three.

I don't understand why this is happening because I'm not using a variable to take away the time, I'm using 1.
Edit:  When reading my post (code) I think I'm setting a new event per start button press, so that is why it is changing like it is.  So now I'm looking to see how I can test to see if an event has been associate with the timer object.  Be back soon I hope.

Here is the code if it will help.  Again, thanks for any help/ideas/pointers/comments.
Code: [Select]

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Diagnostics;
using System.Timers;
using System.Collections;

namespace WindosApps
{
/// <summary>
/// Description of CountDown.
/// </summary>
public class CountDown : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.ListBox listBox1;
System.Timers.Timer CountDownTimer = new System.Timers.Timer();
public CountDown()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();

//
// TODO: Add constructor code after the InitializeComponent() call.
//
}

[STAThread]
public static void Main(string[] args)
{
Application.Run(new CountDown());
}
#region Windows Forms Designer generated code
/// <summary>
/// This method is required for Windows Forms designer support.
/// Do not change the method contents inside the source code editor. The Forms designer might
/// not be able to load this method if it was changed manually.
/// </summary>
private void InitializeComponent() {
this.listBox1 = new System.Windows.Forms.ListBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.button3 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// listBox1
//
this.listBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)));
this.listBox1.BackColor = System.Drawing.SystemColors.Desktop;
this.listBox1.Font = new System.Drawing.Font("Baskerville Old Face", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.listBox1.ForeColor = System.Drawing.SystemColors.Window;
this.listBox1.ItemHeight = 14;
this.listBox1.Location = new System.Drawing.Point(16, 32);
this.listBox1.Name = "listBox1";
this.listBox1.SelectionMode = System.Windows.Forms.SelectionMode.MultiExtended;
this.listBox1.Size = new System.Drawing.Size(216, 186);
this.listBox1.TabIndex = 0;
//
// button1
//
this.button1.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
this.button1.Location = new System.Drawing.Point(16, 328);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(56, 24);
this.button1.TabIndex = 1;
this.button1.Text = "Start";
this.button1.Click += new System.EventHandler(this.Button1Click);
//
// button2
//
this.button2.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
this.button2.Location = new System.Drawing.Point(176, 328);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(56, 24);
this.button2.TabIndex = 1;
this.button2.Text = "Exit";
this.button2.Click += new System.EventHandler(this.Button2Click);
//
// textBox1
//
this.textBox1.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
this.textBox1.BackColor = System.Drawing.SystemColors.Info;
this.textBox1.Font = new System.Drawing.Font("CityBlueprint", 26.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(2)));
this.textBox1.ForeColor = System.Drawing.SystemColors.ActiveCaption;
this.textBox1.Location = new System.Drawing.Point(16, 256);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(216, 52);
this.textBox1.TabIndex = 2;
this.textBox1.Text = "00:00:00";
this.textBox1.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
//
// label1
//
this.label1.Anchor = System.Windows.Forms.AnchorStyles.Top;
this.label1.Location = new System.Drawing.Point(8, 8);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(240, 17);
this.label1.TabIndex = 3;
this.label1.Text = "Highlight processes to end when timer expires.";
//
// label2
//
this.label2.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
this.label2.Location = new System.Drawing.Point(8, 232);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(224, 16);
this.label2.TabIndex = 4;
this.label2.Text = "Time remaining.";
//
// button3
//
this.button3.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
this.button3.Location = new System.Drawing.Point(96, 328);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(56, 24);
this.button3.TabIndex = 1;
this.button3.Text = "Stop";
this.button3.Click += new System.EventHandler(this.Button3Click);
//
// CountDown
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.BackColor = System.Drawing.SystemColors.ActiveBorder;
this.ClientSize = new System.Drawing.Size(248, 365);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button1);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.button2);
this.Controls.Add(this.button3);
this.MaximumSize = new System.Drawing.Size(256, 700);
this.MinimumSize = new System.Drawing.Size(256, 392);
this.Name = "CountDown";
this.Text = "CountDown";
this.Load += new System.EventHandler(this.CountDownLoad);
this.ResumeLayout(false);
}
#endregion
void Button2Click(object sender, System.EventArgs e)
{
CountDownTimer.Dispose();
this.Close();
}

void CountDownLoad(object sender, System.EventArgs e)
{
Process[] ProAr = System.Diagnostics.Process.GetProcesses();
string[] StrAr = new string[ProAr.Length];
int cnt = 0;
foreach (Process Pro in ProAr) {
if (Pro.Responding) {
StrAr[cnt] = Pro.ProcessName;
++cnt;
}
}
Array.Sort (StrAr);
foreach (string Str in StrAr) {
listBox1.Items.Add (Str);
}
}
void Button1Click(object sender, System.EventArgs e)
{
if (this.textBox1.Text.Length != 8) {
MessageBox.Show ("Need to enter time in hours minutes seconds. (00:00:00)");
}
else {
//System.Timers.Timer CountDownTimer = new System.Timers.Timer();
CountDownTimer.Elapsed += new ElapsedEventHandler (SecondCount);
CountDownTimer.Interval = 1000;
CountDownTimer.Enabled = true;
}
}
private void SecondCount (object sender, ElapsedEventArgs e) {
this.textBox1.Text = SubtractSecond (this.textBox1.Text);
if (this.textBox1.Text == "00:00:00") {
foreach (string Str in this.listBox1.SelectedItems) {
foreach (Process Pro in System.Diagnostics.Process.GetProcessesByName (Str)) {
if (!Pro.CloseMainWindow()) {
Pro.Kill();
}
}
}
this.Close();
}
}
private string SubtractSecond (string CurTime) {
int Hour = Convert.ToInt32 (CurTime.Substring (0,2));
int Minute = Convert.ToInt16 (CurTime.Substring (3,2));
int Second = Convert.ToInt16 (CurTime.Substring (6,2));
string strHour = "00";
string strMinute = "00";
string strSecond = "00";
if ((Second == 1 && Minute == 0 && Hour == 0) || (Second == 0 && Minute == 0 && Hour == 0)) {
return "00:00:00";
}
if (Second == 0) {
Minute = Minute - 1;
Second = 59;
if (Minute <= 0 && Hour > 0) {
Hour = Hour - 1;
Minute = 59;
}
}
else {
Second = Second - 1;
}
if (Minute < 0) {
Minute = 0;
}
if (Hour < 0) {
Hour = 0;
}
if (Second < 10) {
strSecond = "0" + Convert.ToString (Second);
}
else {
strSecond = Convert.ToString (Second);
}
if (Minute < 10) {
strMinute = "0" + Convert.ToString (Minute);
}
else {
strMinute = Convert.ToString (Minute);
}
if (Hour < 10) {
strHour = "0" + Convert.ToString (Hour);
}
else {
strHour = Convert.ToString (Hour);
}
return strHour + ":" + strMinute + ":" + strSecond;
}
void Button3Click(object sender, System.EventArgs e)
{
CountDownTimer.Stop();
}

}
}
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

T.Willey

  • Needs a day job
  • Posts: 5247
Re: Trouble with "ElapsedEventHandler"; How to pause it?
« Reply #6 on: September 11, 2006, 05:18:15 PM »

I don't understand why this is happening because I'm not using a variable to take away the time, I'm using 1.
Edit:  When reading my post (code) I think I'm setting a new event per start button press, so that is why it is changing like it is.  So now I'm looking to see how I can test to see if an event has been associate with the timer object.  Be back soon I hope.
This did seem to be the problem.  I moved the assignment of the new 'ElapsedEventHandler' up in the code (in the 'CountDown' section, under 'IinitializeComponent') so that it would only get called once, and it seems to be working.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.