| ■No78039 (straf さん) に返信 > わざわざ書いていただきありがとうございます。 > 少し、理解に時間がかかりそうなので、一度解決済みで終わらせます。
this.components の null チェックが抜けていたので、修正して再掲。
Timer を自前で new するのではなく、デザイナで貼っている場合は 気にしなくて良いことですが、一応念のため。
using System; using System.Runtime.InteropServices; using System.Windows.Forms;
namespace WindowsFormsApplication1 { public partial class Form1 : Form { private Timer timer1; private Label label1; public Form1() { InitializeComponent(); this.label1 = new Label() { Text = "0", AutoSize = true }; this.Controls.Add(this.label1); if (this.components == null) this.components = new System.ComponentModel.Container(); this.timer1 = new Timer(this.components) { Interval = 50 }; this.timer1.Tick += delegate { uint[] last = {8, 0}; GetLastInputInfo(last); var span = new TimeSpan((Environment.TickCount - last[1]) * 10000); this.label1.Text = span.ToString(); if (span.TotalMinutes > 1.0F) { this.timer1.Stop(); MessageBox.Show("1分経過したので終了します"); this.Close(); } }; this.timer1.Start(); }
[DllImport("User32")] private static extern bool GetLastInputInfo(uint[] lastInputInfo); } } |