2011-06-27 12 views
-1

ユーザーが自分のプログラムを開いたときにコードが5分ごとにアクションを開始するコードが必要です。例えば、5分ごとにメッセージアラートが画面に表示されます。 そして、これを止めるコードが必要です。例えば、ユーザーがボタンをクリックすると、このループはそれ以上実行されません。よろしくです。 よろしくお願いします。Windowsアプリケーションの場合、5分ごとに自動的にC#コードを実行する方法。オープンですか?

+0

は、メッセージまたは別のプログラムを表示するプログラムですか? –

+0

警告メッセージを表示しています。 –

+1

これまでのアプローチを投稿してもよろしいですか?このサイトは完全なソリューションを提供するものではなく、特定の問題の解決に役立ちます。それにもかかわらず、Windowsフォームアプリケーションを開発する場合は、System.Windows.Forms.Timerを見てください。 –

答えて

3

他にも言及したように、WinFormsを使用している場合はSystem.Windows.Forms.Timerを使用します。タイマーをツールボックスからフォームにドラッグすると、フォームの下に表示されます。 (ミリ秒に転化する* 1000年5 * 60)Interval = 300000を設定し、Enabled = true

public class Form1 : Form 
    { 
     public Form1() 
     { 
     InitializeComponent(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
     timer1.Enabled = false; 
     } 

     private void timer1_Tick(object sender, EventArgs e) 
     { 
     MessageBox.Show("Annoying message"); 
     } 

     /// <summary> 
     /// Required designer variable. 
     /// </summary> 
     private System.ComponentModel.IContainer components = null; 

     /// <summary> 
     /// Required method for Designer support - do not modify 
     /// the contents of this method with the code editor. 
     /// </summary> 
     private void InitializeComponent() 
     { 
     this.components = new System.ComponentModel.Container(); 
     this.timer1 = new System.Windows.Forms.Timer(this.components); 
     this.button1 = new System.Windows.Forms.Button(); 
     this.SuspendLayout(); 
     // 
     // timer1 
     // 
     this.timer1.Enabled = true; 
     this.timer1.Interval = 300000; 
     this.timer1.Tick += new System.EventHandler(this.timer1_Tick); 
     // 
     // button1 
     // 
     this.button1.Location = new System.Drawing.Point(89, 49); 
     this.button1.Name = "button1"; 
     this.button1.Size = new System.Drawing.Size(75, 23); 
     this.button1.TabIndex = 0; 
     this.button1.Text = "stop timer"; 
     this.button1.UseVisualStyleBackColor = true; 
     this.button1.Click += new System.EventHandler(this.button1_Click); 
     // 
     // Form1 
     // 
     this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 
     this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
     this.ClientSize = new System.Drawing.Size(292, 266); 
     this.Controls.Add(this.button1); 
     this.Name = "Form1"; 
     this.Text = "Form1"; 
     this.ResumeLayout(false); 

     } 

     #endregion 

     private System.Windows.Forms.Timer timer1; 
     private System.Windows.Forms.Button button1; 
} 
1

Timerクラスを使用して、Elapsedイベントをバインドして作業を行うことができます。

0

アクションは、ユーザーが実行しているのと同じプログラムで実行できると仮定して、タイマーコントロールをチェックしてください。正確な名前とアプローチは、お使いのプラットフォームによって異なる場合があります。

0

原始的な解決方法については、TimersMessageBox.Showをご覧ください。

+0

私はあなたの答えをありがとう。 –

関連する問題