A THINKING FRUIT > C#

 SimpleTimer(シンプルなスレッドの使用方法)
※ 記事としては未定完成です。
Alertform.cs
using System;
using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace SingleTimer {     public partial class Alertform : Form     {         public Alertform( string strMessage, string strTitle )         {             InitializeComponent();             buttonClose.Click += ButtonClose_Click;             textMessage.Text = strMessage;             textMessage.SelectionLength = 0;             textMessage.SelectionStart = 0;             this.Text = strTitle;         }         private void ButtonClose_Click(object sender, EventArgs e)         {             this.Close();         }     } }
Alertform.Designer.cs
namespace SingleTimer {     partial class Alertform     {         /// <summary>         /// Required designer variable.         /// </summary>         private System.ComponentModel.IContainer components = null;         /// <summary>         /// Clean up any resources being used.         /// </summary>         /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>         protected override void Dispose(bool disposing)         {             if (disposing && (components != null))             {                 components.Dispose();             }             base.Dispose(disposing);         }         #region Windows Form Designer generated code         /// <summary>         /// Required method for Designer support - do not modify         /// the contents of this method with the code editor.         /// </summary>         private void InitializeComponent()         {             this.textMessage = new System.Windows.Forms.TextBox();             this.label1 = new System.Windows.Forms.Label();             this.buttonClose = new System.Windows.Forms.Button();             this.SuspendLayout();             //             // textMessage             //             this.textMessage.Font = new System.Drawing.Font("MS ゴシック", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(128)));             this.textMessage.Location = new System.Drawing.Point(11, 26);             this.textMessage.Margin = new System.Windows.Forms.Padding(4);             this.textMessage.Multiline = true;             this.textMessage.Name = "textMessage";             this.textMessage.ReadOnly = true;             this.textMessage.ScrollBars = System.Windows.Forms.ScrollBars.Both;             this.textMessage.Size = new System.Drawing.Size(359, 118);             this.textMessage.TabIndex = 0;             //             // label1             //             this.label1.AutoSize = true;             this.label1.Location = new System.Drawing.Point(13, 7);             this.label1.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);             this.label1.Name = "label1";             this.label1.Size = new System.Drawing.Size(62, 15);             this.label1.TabIndex = 1;             this.label1.Text = "メッセージ";             //             // buttonClose             //             this.buttonClose.Location = new System.Drawing.Point(139, 149);             this.buttonClose.Margin = new System.Windows.Forms.Padding(2);             this.buttonClose.Name = "buttonClose";             this.buttonClose.Size = new System.Drawing.Size(95, 33);             this.buttonClose.TabIndex = 2;             this.buttonClose.Text = "閉じる";             this.buttonClose.UseVisualStyleBackColor = true;             //             // Alertform             //             this.AcceptButton = this.buttonClose;             this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;             this.ClientSize = new System.Drawing.Size(381, 192);             this.Controls.Add(this.buttonClose);             this.Controls.Add(this.label1);             this.Controls.Add(this.textMessage);             this.Font = new System.Drawing.Font("MS UI Gothic", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(128)));             this.Margin = new System.Windows.Forms.Padding(4);             this.MaximizeBox = false;             this.MinimizeBox = false;             this.Name = "Alertform";             this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;             this.Text = "Alertform";             this.TopMost = true;             this.ResumeLayout(false);             this.PerformLayout();         }         #endregion         private System.Windows.Forms.TextBox textMessage;         private System.Windows.Forms.Label label1;         private System.Windows.Forms.Button buttonClose;     } }
Mainform.cs
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace SingleTimer {     public partial class Mainform : Form     {         public readonly string NL = System.Environment.NewLine;         public Mainform()         {             InitializeComponent();             buttonAction.Click += ButtonAction_Click;             buttonStop.Click += ButtonStop_Click;             buttonStop.Enabled = false;         }         protected override void OnClosed( EventArgs e )         {             m_eventCancellation.Dispose();         }         private void ButtonStop_Click(object sender, EventArgs e)         {             if( !m_eventCancellation.IsSet )             {                 m_eventCancellation.Set();             }         }         System.Threading.ManualResetEventSlim m_eventCancellation = new System.Threading.ManualResetEventSlim();         bool m_bAction = false;         private void EnableControl( bool bAction )         {             buttonAction.Enabled = m_bAction;             m_bAction = bAction;             buttonStop.Enabled = m_bAction;             textMemo.ReadOnly = m_bAction;             textTimeM.ReadOnly = m_bAction;         }         private async void ButtonAction_Click(object sender, EventArgs e)         {             if( m_bAction )             {                 return;             }             string strMessage = "";             int nTimeM;             if( !int.TryParse(textTimeM.Text, out nTimeM) || nTimeM <= 0 )             {                 strMessage += "時間指定が不正です。(1分以上必要です)" + NL;             }             if( textMemo.Text.Length == 0 )             {                 strMessage += "メモが不正です。(1文字を以上必要です)" + NL;             }             if( strMessage.Length > 0 )             {                 using( var alert = new Alertform( strMessage, Text ) )                 {                     alert.ShowDialog( this );                 }                 return;             }             EnableControl( true );             this.WindowState = FormWindowState.Minimized;             nTimeM *= 1000 * 60;             m_eventCancellation.Reset();             System.Action<bool> fnShowMessage = ( bool bSignal ) =>             {                 // bSignal == true はキャンセルした時                 if( bSignal == false )                 {                     using( var alert = new Alertform( "指定時間を経過しました。" + NL + NL + textMemo.Text, Text ) )                     {                         alert.ShowDialog();                     }                 }                 EnableControl( false );                 if( this.WindowState == FormWindowState.Minimized )                 {                     this.WindowState = FormWindowState.Normal;                 }             };             await Task.Run( () =>             {                 bool bSignal = m_eventCancellation.Wait( nTimeM );                 this.Invoke( fnShowMessage, bSignal );             } );         }     } }
Mainform.Designer.cs
namespace SingleTimer {     partial class Mainform     {         /// <summary>         /// 必要なデザイナー変数です。         /// </summary>         private System.ComponentModel.IContainer components = null;         /// <summary>         /// 使用中のリソースをすべてクリーンアップします。         /// </summary>         /// <param name="disposing">マネージド リソースを破棄する場合は true を指定し、その他の場合は false を指定します。</param>         protected override void Dispose(bool disposing)         {             if (disposing && (components != null))             {                 components.Dispose();             }             base.Dispose(disposing);         }         #region Windows フォーム デザイナーで生成されたコード         /// <summary>         /// デザイナー サポートに必要なメソッドです。このメソッドの内容を         /// コード エディターで変更しないでください。         /// </summary>         private void InitializeComponent()         {             this.buttonAction = new System.Windows.Forms.Button();             this.textMemo = new System.Windows.Forms.TextBox();             this.textTimeM = new System.Windows.Forms.TextBox();             this.labelTime = new System.Windows.Forms.Label();             this.labelMemo = new System.Windows.Forms.Label();             this.buttonStop = new System.Windows.Forms.Button();             this.labelMinutes = new System.Windows.Forms.Label();             this.SuspendLayout();             //             // buttonAction             //             this.buttonAction.Anchor = System.Windows.Forms.AnchorStyles.Bottom;             this.buttonAction.Location = new System.Drawing.Point(188, 131);             this.buttonAction.Name = "buttonAction";             this.buttonAction.Size = new System.Drawing.Size(75, 23);             this.buttonAction.TabIndex = 5;             this.buttonAction.Text = "実行";             this.buttonAction.UseVisualStyleBackColor = true;             //             // textMemo             //             this.textMemo.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right)));             this.textMemo.Location = new System.Drawing.Point(12, 25);             this.textMemo.Multiline = true;             this.textMemo.Name = "textMemo";             this.textMemo.Size = new System.Drawing.Size(419, 100);             this.textMemo.TabIndex = 1;             //             // textTimeM             //             this.textTimeM.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));             this.textTimeM.Font = new System.Drawing.Font("MS ゴシック", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(128)));             this.textTimeM.Location = new System.Drawing.Point(46, 132);             this.textTimeM.Name = "textTimeM";             this.textTimeM.Size = new System.Drawing.Size(71, 22);             this.textTimeM.TabIndex = 3;             //             // labelTime             //             this.labelTime.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));             this.labelTime.AutoSize = true;             this.labelTime.Location = new System.Drawing.Point(13, 138);             this.labelTime.Name = "labelTime";             this.labelTime.Size = new System.Drawing.Size(29, 12);             this.labelTime.TabIndex = 2;             this.labelTime.Text = "時間";             //             // labelMemo             //             this.labelMemo.AutoSize = true;             this.labelMemo.Location = new System.Drawing.Point(10, 10);             this.labelMemo.Name = "labelMemo";             this.labelMemo.Size = new System.Drawing.Size(22, 12);             this.labelMemo.TabIndex = 0;             this.labelMemo.Text = "メモ";             //             // buttonStop             //             this.buttonStop.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));             this.buttonStop.DialogResult = System.Windows.Forms.DialogResult.Cancel;             this.buttonStop.Location = new System.Drawing.Point(356, 131);             this.buttonStop.Name = "buttonStop";             this.buttonStop.Size = new System.Drawing.Size(75, 23);             this.buttonStop.TabIndex = 6;             this.buttonStop.Text = "停止";             this.buttonStop.UseVisualStyleBackColor = true;             //             // labelMinutes             //             this.labelMinutes.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));             this.labelMinutes.AutoSize = true;             this.labelMinutes.Location = new System.Drawing.Point(122, 138);             this.labelMinutes.Name = "labelMinutes";             this.labelMinutes.Size = new System.Drawing.Size(25, 12);             this.labelMinutes.TabIndex = 4;             this.labelMinutes.Text = "[分]";             //             // Mainform             //             this.AcceptButton = this.buttonAction;             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;             this.CancelButton = this.buttonStop;             this.ClientSize = new System.Drawing.Size(446, 161);             this.Controls.Add(this.buttonStop);             this.Controls.Add(this.labelMemo);             this.Controls.Add(this.labelMinutes);             this.Controls.Add(this.labelTime);             this.Controls.Add(this.textTimeM);             this.Controls.Add(this.textMemo);             this.Controls.Add(this.buttonAction);             this.Name = "Mainform";             this.Text = "SingleTimer";             this.ResumeLayout(false);             this.PerformLayout();         }         #endregion         private System.Windows.Forms.Button buttonAction;         private System.Windows.Forms.TextBox textMemo;         private System.Windows.Forms.TextBox textTimeM;         private System.Windows.Forms.Label labelTime;         private System.Windows.Forms.Label labelMemo;         private System.Windows.Forms.Button buttonStop;         private System.Windows.Forms.Label labelMinutes;     } }
Program.cs
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms; namespace SingleTimer {     static class Program     {         /// <summary>         /// アプリケーションのメイン エントリ ポイントです。         /// </summary>         [STAThread]         static void Main()         {             Application.EnableVisualStyles();             Application.SetCompatibleTextRenderingDefault(false);             Application.Run(new Mainform());         }     } }