diff --git a/TheFilenalCountdown/Form1.Designer.cs b/TheFilenalCountdown/Form1.Designer.cs index 21b5608..15b5829 100644 --- a/TheFilenalCountdown/Form1.Designer.cs +++ b/TheFilenalCountdown/Form1.Designer.cs @@ -48,6 +48,7 @@ private void InitializeComponent() this.cbx_format = new System.Windows.Forms.ComboBox(); this.chk_countUp = new System.Windows.Forms.CheckBox(); this.label6 = new System.Windows.Forms.Label(); + this.btn_stop = new System.Windows.Forms.Button(); this.grp_time.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.num_seconds)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.num_minutes)).BeginInit(); @@ -63,26 +64,23 @@ private void InitializeComponent() this.grp_time.Controls.Add(this.label4); this.grp_time.Controls.Add(this.label3); this.grp_time.Controls.Add(this.label2); - this.grp_time.Location = new System.Drawing.Point(38, 165); - this.grp_time.Margin = new System.Windows.Forms.Padding(10, 9, 10, 9); + this.grp_time.Location = new System.Drawing.Point(12, 58); this.grp_time.Name = "grp_time"; - this.grp_time.Padding = new System.Windows.Forms.Padding(10, 9, 10, 9); - this.grp_time.Size = new System.Drawing.Size(507, 410); + this.grp_time.Size = new System.Drawing.Size(160, 144); this.grp_time.TabIndex = 0; this.grp_time.TabStop = false; this.grp_time.Text = "Time Until Zero"; // // num_seconds // - this.num_seconds.Location = new System.Drawing.Point(231, 293); - this.num_seconds.Margin = new System.Windows.Forms.Padding(10, 9, 10, 9); + this.num_seconds.Location = new System.Drawing.Point(73, 103); this.num_seconds.Maximum = new decimal(new int[] { 1000, 0, 0, 0}); this.num_seconds.Name = "num_seconds"; - this.num_seconds.Size = new System.Drawing.Size(241, 44); + this.num_seconds.Size = new System.Drawing.Size(76, 20); this.num_seconds.TabIndex = 4; this.num_seconds.Value = new decimal(new int[] { 10, @@ -92,66 +90,60 @@ private void InitializeComponent() // // num_minutes // - this.num_minutes.Location = new System.Drawing.Point(231, 191); - this.num_minutes.Margin = new System.Windows.Forms.Padding(10, 9, 10, 9); + this.num_minutes.Location = new System.Drawing.Point(73, 67); this.num_minutes.Maximum = new decimal(new int[] { 1000, 0, 0, 0}); this.num_minutes.Name = "num_minutes"; - this.num_minutes.Size = new System.Drawing.Size(241, 44); + this.num_minutes.Size = new System.Drawing.Size(76, 20); this.num_minutes.TabIndex = 3; // // num_hours // - this.num_hours.Location = new System.Drawing.Point(231, 97); - this.num_hours.Margin = new System.Windows.Forms.Padding(10, 9, 10, 9); + this.num_hours.Location = new System.Drawing.Point(73, 34); this.num_hours.Maximum = new decimal(new int[] { 24, 0, 0, 0}); this.num_hours.Name = "num_hours"; - this.num_hours.Size = new System.Drawing.Size(241, 44); + this.num_hours.Size = new System.Drawing.Size(76, 20); this.num_hours.TabIndex = 2; // // label4 // this.label4.AutoSize = true; - this.label4.Location = new System.Drawing.Point(48, 302); - this.label4.Margin = new System.Windows.Forms.Padding(10, 0, 10, 0); + this.label4.Location = new System.Drawing.Point(15, 106); this.label4.Name = "label4"; - this.label4.Size = new System.Drawing.Size(141, 37); + this.label4.Size = new System.Drawing.Size(49, 13); this.label4.TabIndex = 10; this.label4.Text = "Seconds"; // // label3 // this.label3.AutoSize = true; - this.label3.Location = new System.Drawing.Point(48, 202); - this.label3.Margin = new System.Windows.Forms.Padding(10, 0, 10, 0); + this.label3.Location = new System.Drawing.Point(15, 71); this.label3.Name = "label3"; - this.label3.Size = new System.Drawing.Size(128, 37); + this.label3.Size = new System.Drawing.Size(44, 13); this.label3.TabIndex = 8; this.label3.Text = "Minutes"; // // label2 // this.label2.AutoSize = true; - this.label2.Location = new System.Drawing.Point(48, 102); - this.label2.Margin = new System.Windows.Forms.Padding(10, 0, 10, 0); + this.label2.Location = new System.Drawing.Point(15, 36); this.label2.Name = "label2"; - this.label2.Size = new System.Drawing.Size(103, 37); + this.label2.Size = new System.Drawing.Size(35, 13); this.label2.TabIndex = 6; this.label2.Text = "Hours"; // // btn_start // - this.btn_start.Location = new System.Drawing.Point(393, 605); - this.btn_start.Margin = new System.Windows.Forms.Padding(10, 9, 10, 9); + this.btn_start.Location = new System.Drawing.Point(124, 213); this.btn_start.Name = "btn_start"; - this.btn_start.Size = new System.Drawing.Size(238, 65); + this.btn_start.Size = new System.Drawing.Size(75, 23); this.btn_start.TabIndex = 9; this.btn_start.Text = "Start"; this.btn_start.UseVisualStyleBackColor = true; @@ -159,10 +151,9 @@ private void InitializeComponent() // // btn_chooseFile // - this.btn_chooseFile.Location = new System.Drawing.Point(38, 54); - this.btn_chooseFile.Margin = new System.Windows.Forms.Padding(10, 9, 10, 9); + this.btn_chooseFile.Location = new System.Drawing.Point(12, 19); this.btn_chooseFile.Name = "btn_chooseFile"; - this.btn_chooseFile.Size = new System.Drawing.Size(238, 65); + this.btn_chooseFile.Size = new System.Drawing.Size(75, 23); this.btn_chooseFile.TabIndex = 1; this.btn_chooseFile.Text = "Output File"; this.btn_chooseFile.UseVisualStyleBackColor = true; @@ -171,10 +162,9 @@ private void InitializeComponent() // lbl_filename // this.lbl_filename.AutoSize = true; - this.lbl_filename.Location = new System.Drawing.Point(298, 68); - this.lbl_filename.Margin = new System.Windows.Forms.Padding(10, 0, 10, 0); + this.lbl_filename.Location = new System.Drawing.Point(94, 24); this.lbl_filename.Name = "lbl_filename"; - this.lbl_filename.Size = new System.Drawing.Size(94, 37); + this.lbl_filename.Size = new System.Drawing.Size(33, 13); this.lbl_filename.TabIndex = 8; this.lbl_filename.Text = "None"; // @@ -185,11 +175,9 @@ private void InitializeComponent() this.groupBox1.Controls.Add(this.label1); this.groupBox1.Controls.Add(this.cbx_capitalization); this.groupBox1.Controls.Add(this.cbx_format); - this.groupBox1.Location = new System.Drawing.Point(564, 165); - this.groupBox1.Margin = new System.Windows.Forms.Padding(10, 9, 10, 9); + this.groupBox1.Location = new System.Drawing.Point(178, 58); this.groupBox1.Name = "groupBox1"; - this.groupBox1.Padding = new System.Windows.Forms.Padding(10, 9, 10, 9); - this.groupBox1.Size = new System.Drawing.Size(707, 410); + this.groupBox1.Size = new System.Drawing.Size(223, 144); this.groupBox1.TabIndex = 11; this.groupBox1.TabStop = false; this.groupBox1.Text = "Format"; @@ -197,10 +185,9 @@ private void InitializeComponent() // label5 // this.label5.AutoSize = true; - this.label5.Location = new System.Drawing.Point(51, 302); - this.label5.Margin = new System.Windows.Forms.Padding(10, 0, 10, 0); + this.label5.Location = new System.Drawing.Point(16, 106); this.label5.Name = "label5"; - this.label5.Size = new System.Drawing.Size(353, 37); + this.label5.Size = new System.Drawing.Size(118, 13); this.label5.TabIndex = 13; this.label5.Text = "Replace Commas With "; // @@ -213,19 +200,17 @@ private void InitializeComponent() " /", " :", " &"}); - this.cbx_replaceCommasWith.Location = new System.Drawing.Point(443, 293); - this.cbx_replaceCommasWith.Margin = new System.Windows.Forms.Padding(10, 9, 10, 9); + this.cbx_replaceCommasWith.Location = new System.Drawing.Point(140, 103); this.cbx_replaceCommasWith.Name = "cbx_replaceCommasWith"; - this.cbx_replaceCommasWith.Size = new System.Drawing.Size(194, 45); + this.cbx_replaceCommasWith.Size = new System.Drawing.Size(64, 21); this.cbx_replaceCommasWith.TabIndex = 7; // // label1 // this.label1.AutoSize = true; - this.label1.Location = new System.Drawing.Point(51, 202); - this.label1.Margin = new System.Windows.Forms.Padding(10, 0, 10, 0); + this.label1.Location = new System.Drawing.Point(16, 71); this.label1.Name = "label1"; - this.label1.Size = new System.Drawing.Size(210, 37); + this.label1.Size = new System.Drawing.Size(69, 13); this.label1.TabIndex = 11; this.label1.Text = "Capitalization"; // @@ -237,10 +222,9 @@ private void InitializeComponent() "all lowercase", "ALL CAPS", "Inital Caps"}); - this.cbx_capitalization.Location = new System.Drawing.Point(288, 194); - this.cbx_capitalization.Margin = new System.Windows.Forms.Padding(10, 9, 10, 9); + this.cbx_capitalization.Location = new System.Drawing.Point(91, 68); this.cbx_capitalization.Name = "cbx_capitalization"; - this.cbx_capitalization.Size = new System.Drawing.Size(349, 45); + this.cbx_capitalization.Size = new System.Drawing.Size(113, 21); this.cbx_capitalization.TabIndex = 6; // // cbx_format @@ -254,18 +238,18 @@ private void InitializeComponent() "mm:ss", "mm minutes, ss seconds", "mm mins, ss secs"}); - this.cbx_format.Location = new System.Drawing.Point(60, 97); - this.cbx_format.Margin = new System.Windows.Forms.Padding(10, 9, 10, 9); + this.cbx_format.Location = new System.Drawing.Point(19, 34); this.cbx_format.Name = "cbx_format"; - this.cbx_format.Size = new System.Drawing.Size(577, 45); + this.cbx_format.Size = new System.Drawing.Size(185, 21); this.cbx_format.TabIndex = 5; // // chk_countUp // this.chk_countUp.AutoSize = true; - this.chk_countUp.Location = new System.Drawing.Point(38, 618); + this.chk_countUp.Location = new System.Drawing.Point(12, 217); + this.chk_countUp.Margin = new System.Windows.Forms.Padding(1, 1, 1, 1); this.chk_countUp.Name = "chk_countUp"; - this.chk_countUp.Size = new System.Drawing.Size(312, 41); + this.chk_countUp.Size = new System.Drawing.Size(109, 17); this.chk_countUp.TabIndex = 8; this.chk_countUp.Text = "Count Up Instead"; this.chk_countUp.UseVisualStyleBackColor = true; @@ -273,17 +257,29 @@ private void InitializeComponent() // label6 // this.label6.AutoSize = true; - this.label6.Location = new System.Drawing.Point(916, 619); + this.label6.Location = new System.Drawing.Point(289, 217); + this.label6.Margin = new System.Windows.Forms.Padding(1, 0, 1, 0); this.label6.Name = "label6"; - this.label6.Size = new System.Drawing.Size(260, 37); + this.label6.Size = new System.Drawing.Size(90, 13); this.label6.TabIndex = 12; this.label6.Text = "git.io/JJPOS v1.3"; // + // btn_stop + // + this.btn_stop.Location = new System.Drawing.Point(205, 213); + this.btn_stop.Name = "btn_stop"; + this.btn_stop.Size = new System.Drawing.Size(75, 23); + this.btn_stop.TabIndex = 13; + this.btn_stop.Text = "Stop"; + this.btn_stop.UseVisualStyleBackColor = true; + this.btn_stop.Click += new System.EventHandler(this.btn_stop_Click); + // // Form1 // - this.AutoScaleDimensions = new System.Drawing.SizeF(19F, 37F); + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(1347, 714); + this.ClientSize = new System.Drawing.Size(425, 251); + this.Controls.Add(this.btn_stop); this.Controls.Add(this.label6); this.Controls.Add(this.chk_countUp); this.Controls.Add(this.groupBox1); @@ -293,7 +289,6 @@ private void InitializeComponent() this.Controls.Add(this.grp_time); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); - this.Margin = new System.Windows.Forms.Padding(10, 9, 10, 9); this.Name = "Form1"; this.Text = "The Filenal Countdown"; this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing_1); @@ -330,6 +325,7 @@ private void InitializeComponent() private System.Windows.Forms.ComboBox cbx_replaceCommasWith; private System.Windows.Forms.CheckBox chk_countUp; private System.Windows.Forms.Label label6; + private System.Windows.Forms.Button btn_stop; } } diff --git a/TheFilenalCountdown/Form1.cs b/TheFilenalCountdown/Form1.cs index 1c8f84a..ed5d12b 100644 --- a/TheFilenalCountdown/Form1.cs +++ b/TheFilenalCountdown/Form1.cs @@ -14,18 +14,18 @@ namespace TheFilenalCountdown { public partial class Form1 : Form { - static int totalSeconds; - static TimeSpan timeSpan; - static String selectedFormatString; - static String outputFilename; + int totalSeconds; + TimeSpan timeSpan; + String selectedFormatString; + String outputFilename; - static System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer(); - static int secondsCounted = 0; - static bool exitFlag = false; - static bool countUp = false; + // not using System.Windows.Forms.Timer because it has bad precision + System.Timers.Timer myTimer = new System.Timers.Timer(); + int secondsCounted = 0; + bool countUp = false; // This is the method to run when the timer is raised, every second - private static void TimerEventProcessor(Object myObject, EventArgs myEventArgs) + private void TimerEventProcessor(Object myObject, EventArgs myEventArgs) { secondsCounted++; @@ -35,16 +35,22 @@ private static void TimerEventProcessor(Object myObject, EventArgs myEventArgs) System.IO.File.WriteAllText(outputFilename, String.Format(selectedFormatString, timeSpan)); //Console.WriteLine(selectedFormatString, timeSpan); - if (secondsCounted == totalSeconds) // Stop the timer. - exitFlag = true; + if (secondsCounted == totalSeconds) + { + stopTimer(); + } } public Form1() { InitializeComponent(); - myTimer.Tick += new EventHandler(TimerEventProcessor); + myTimer.Elapsed += new System.Timers.ElapsedEventHandler(TimerEventProcessor); myTimer.Interval = 1000; + + // makes the timer ElipsedEventHandler be invoked on the same thread as this form, which + // means we can safely access all form controls from inside + myTimer.SynchronizingObject = this; ArrayList timeFormats = new ArrayList(); timeFormats.Add(new timeFormatSelection("hh:mm:ss", "{0:hh\\:mm\\:ss}")); @@ -73,6 +79,8 @@ public Form1() cbx_format.SelectedIndex = Properties.Settings.Default.formatIndex; cbx_capitalization.SelectedIndex = Properties.Settings.Default.capsIndex; cbx_replaceCommasWith.Text = Properties.Settings.Default.commasText; + + btn_stop.Enabled = false; } private void chooseFile_Click(object sender, EventArgs e) @@ -92,7 +100,17 @@ private void chooseFile_Click(object sender, EventArgs e) private void start_Click(object sender, EventArgs e) { - countUp = chk_countUp.Checked; + startTimer(); + } + + private void btn_stop_Click(object sender, EventArgs e) + { + stopTimer(); + } + + private void startTimer() + { + countUp = chk_countUp.Checked; selectedFormatString = (String)cbx_format.SelectedValue; if (lbl_filename.Text == "None") @@ -112,7 +130,7 @@ private void start_Click(object sender, EventArgs e) // this is a little clumsy, but I'm okay with it (trying not to wreck the hh, mm, etc) if (cbx_capitalization.SelectedIndex == 1) // ALL CAPS - if (selectedFormatString.Contains("hours")) + if (selectedFormatString.Contains("hours")) selectedFormatString = selectedFormatString.Replace(" hours", " HOURS").Replace(" minutes", " MINUTES").Replace(" seconds", " SECONDS"); else selectedFormatString = selectedFormatString.Replace(" hrs", " HRS").Replace(" mins", " MINS").Replace(" secs", " SECS"); @@ -123,27 +141,19 @@ private void start_Click(object sender, EventArgs e) selectedFormatString = selectedFormatString.Replace(",", cbx_replaceCommasWith.Text); btn_start.Enabled = false; - btn_start.Text = "Counting"; + btn_stop.Enabled = true; myTimer.Start(); + } + } - while (exitFlag == false) - { - // Yield CPU time to other processes - Thread.Sleep(10); - - // Processes all the events in the queue. - Application.DoEvents(); - } - - // the series of 1 second events is done, given the time that the user specified - myTimer.Stop(); - exitFlag = false; - secondsCounted = 0; + private void stopTimer() + { + myTimer.Stop(); - btn_start.Text = "Start"; - btn_start.Enabled = true; - } + secondsCounted = 0; + btn_start.Enabled = true; + btn_stop.Enabled = false; } private void Form1_FormClosing_1(object sender, FormClosingEventArgs e) @@ -158,9 +168,6 @@ private void Form1_FormClosing_1(object sender, FormClosingEventArgs e) } else { - myTimer.Stop(); - myTimer.Dispose(); - Properties.Settings.Default.hrs = num_hours.Value; Properties.Settings.Default.mins = num_minutes.Value; Properties.Settings.Default.secs = num_seconds.Value; @@ -169,13 +176,8 @@ private void Form1_FormClosing_1(object sender, FormClosingEventArgs e) Properties.Settings.Default.capsIndex = cbx_capitalization.SelectedIndex; Properties.Settings.Default.commasText = cbx_replaceCommasWith.Text; Properties.Settings.Default.Save(); - - Application.ExitThread(); - Environment.Exit(0); - } } - }