C# 线程操作
using System; using System.Collections.Generic; using System.ComponentModel; using System.Configuration; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading; using BLL; using Model; using log4net; using System.Windows.Forms; namespace TerminalAccessService { public partial class TerminalAccessForm : Form { private readonly ILog _log; AlarmInfoBLL _alarmInfoBll = new AlarmInfoBLL(); AuditRecordsBLL _auditRecordsBll = new AuditRecordsBLL(); AuditRecordsHistoryBLL _auditRecordsHistoryBll = new AuditRecordsHistoryBLL(); AuditStrategyInfoBLL _auditStrategyInfoBll = new AuditStrategyInfoBLL(); AuditAccessClientInfoBLL _auditAccessClientInfoBll = new AuditAccessClientInfoBLL(); VlanBLL _vlanBll = new VlanBLL(); private Thread thread; private bool flag = true; public TerminalAccessForm() { InitializeComponent(); try { thread = new Thread(Init); } catch (Exception) { flag = false; } _log = LogManager.GetLogger("TerminalAccessForm"); } /// <summary> /// 初始化线程 /// </summary> public void Init() { while (flag) { ExecuteFunction(); Thread.Sleep(5000); } } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btn_start_Click(object sender, EventArgs e) { thread.Start(); tbx_content.Text += "正在启动服务,请稍候..."; } /// <summary> /// 线程固定时间执行方法 /// </summary> public void ExecuteFunction() { try { //从sqlserver获取数据 var data = _alarmInfoBll.Query(); foreach (var alarmInfo in data) { ReadData(alarmInfo); ProcessData(alarmInfo); } } catch (Exception) { flag = false; } } /// <summary> /// 过程一:读取数据 /// </summary> /// <param name="alarmInfo"></param> public void ReadData(AlarmInfo alarmInfo) { //从mysql获取数据 var tmp = _auditRecordsBll.IsExist(new AuditRecord() { Ip = alarmInfo.Ip, Mac = alarmInfo.Mac, ClrType = alarmInfo.Type }); if (tmp != null) { tmp.ClrTime = DateTime.Now.ToString(); _auditRecordsBll.Update(tmp); _auditRecordsHistoryBll.Save(new AuditRecordsHistory() { Ip = alarmInfo.Ip, Mac = alarmInfo.Mac, ClrType = alarmInfo.Type, ClrMsg = alarmInfo.Msg, ClrTime = alarmInfo.AlarmTime }); } else { _auditRecordsBll.Save(new AuditRecord() { Ip = alarmInfo.Ip, Mac = alarmInfo.Mac, ClrType = alarmInfo.Type, ClrMsg = alarmInfo.Msg, ClrTime = alarmInfo.AlarmTime.ToString() }); } } /// <summary> /// 过程二:处理数据 /// </summary> /// <param name="alarmInfo"></param> public void ProcessData(AlarmInfo alarmInfo) { var data2 = _auditStrategyInfoBll.QueryByType(alarmInfo.Type); var memo = ""; if (data2.IsAccess == 0) { memo = "自动禁止准入"; var ipManService = new IpManService.IpManService(); var data = _vlanBll.QueryCoreSwitch(alarmInfo.Ip); foreach (var vlanBindDefaultSwtich in data) { var tmp = ipManService.DisableIp(alarmInfo.Ip, alarmInfo.Mac, vlanBindDefaultSwtich.Switch); } } _auditAccessClientInfoBll.Save(new AuditAccessClientInfo() { ClientIp = alarmInfo.Ip, ClientMac = alarmInfo.Mac, ClientName = alarmInfo.DeviceName, IsAccess = int.Parse(alarmInfo.Type), Memo = memo }); } private void btn_end_Click(object sender, EventArgs e) { thread.Abort(); tbx_content.Text += "\r\n正在关闭服务,请稍候...\r\n"; } } }