using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace Monitor
{
public partial class FrmMain : Form
{
public FrmMain()
{
InitializeComponent();
}
Thread th = null;
public bool isrunning = false;
private void 启动监视ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (runStatusLabel.Text == "停止中")
{
runStatusLabel.Text = "运行中";
th = new Thread(new ThreadStart(run));
th.IsBackground = true;
th.SetApartmentState(ApartmentState.STA);
isrunning = true;
th.Start();
}
}
private void 停止监视ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (runStatusLabel.Text == "运行中")
{
runStatusLabel.Text = "停止中";
isrunning = false;
th.Abort();
}
}
/// <summary>
/// 线程开启方法
/// </summary>
protected void run()
{
while (isrunning)
{
this.BeginInvoke(new EventHandler(delegate
{
TimeStatusLabel.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
}));
Console.WriteLine(DateTime.Now.ToString());
Thread.Sleep(1000);
}
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace Monitor
{
public partial class FrmMain : Form
{
public FrmMain()
{
InitializeComponent();
}
Thread th = null;
public bool isrunning = false;
private void 启动监视ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (runStatusLabel.Text == "停止中")
{
runStatusLabel.Text = "运行中";
th = new Thread(new ThreadStart(run));
th.IsBackground = true;
th.SetApartmentState(ApartmentState.STA);
isrunning = true;
th.Start();
}
}
private void 停止监视ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (runStatusLabel.Text == "运行中")
{
runStatusLabel.Text = "停止中";
isrunning = false;
th.Abort();
}
}
/// <summary>
/// 线程开启方法
/// </summary>
protected void run()
{
while (isrunning)
{
this.BeginInvoke(new EventHandler(delegate
{
TimeStatusLabel.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
}));
Console.WriteLine(DateTime.Now.ToString());
Thread.Sleep(1000);
}
}
}
}