C# Timer 运行一段时间不触发Tick ,一段时间后又触发Tick
代码如下
From1.cs
using Ini_test;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using static 闹铃.MyEvents;
namespace 闹铃
{
public partial class Form1 : Form
{
public static string Config_file_Path = Application.StartupPath + @"\Config.ini";
public static string SerialPortName;
public static int SerialPortBaudRate;
public static bool Alarm_RunState; //闹铃运行状态
public static bool Alarm_Enable; //闹铃是否启用
public static string MeterAddress; //通信地址
public static bool ExitApplication = false; //软件退出
private bool isReceived698Event = false; //接收数据标志
private int protocol = 0; //645/698
private static string Last_DateTime; //最后时间
MyEvents myEvents = new MyEvents(); //新建事件
MyEvents myClearEvents = new MyEvents(); //新建清零事件
MyEvents myAdjustEvents = new MyEvents(); //广播校时
string BiDuiTime = "";
public struct Received_Struct
{
public int indexer;//
public byte[] buffer;
}
Received_Struct ReceivedFrame_Struct_698 = new Received_Struct();
struct Time_consumption_Struct
{
public string Time;
public int Deley;
}
Time_consumption_Struct[] Time_consumption_Structure;
enum SerialPortState_Enum
{
Waiting,
Sending,
Sended,
Receiving,
Received
};
SerialPortState_Enum serialPortState_Enum = new SerialPortState_Enum();
private bool GetTime_consumption(string str)
{
str = str.Trim(';').Replace("\r", ";").Replace("\n","");
string[] times = str.Split(';');
Time_consumption_Struct[] Temp_Structure = new Time_consumption_Struct[times.Length];
int index = 0;
for (int i = 0; i < times.Length; i++)
{
string[] Time_con = times[i].Split('-');
if (Time_con.Length == 2)
{
try
{
TimeSpan t = Convert.ToDateTime(Time_con[0]).TimeOfDay;
Temp_Structure[index].Time = Time_con[0];
Temp_Structure[index].Deley = Convert.ToInt16(Time_con[1]);
index++;
}
catch (Exception)
{
//throw;
}
}
}
Time_consumption_Structure = new Time_consumption_Struct[index];
for (int i = 0; i < index; i++)
{
Time_consumption_Structure[i].Time = Temp_Structure[i].Time;
Time_consumption_Structure[i].Deley = Temp_Structure[i].Deley;
}
return true;
}
public Form1()
{
InitializeComponent();
}
private void systemSetToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
if (form2.ShowDialog() == DialogResult.OK)
{
MeterAddress = InI.ReadIni("Meter", "Address", "", Config_file_Path);
serialPort1.BaudRate = Convert.ToInt16( InI.ReadIni("SerialPort", "BaudRate", "", Config_file_Path));
serialPort1.PortName = InI.ReadIni("SerialPort", "PortName", "", Config_file_Path);
}
}
private void controlEnableToolStripMenuItem_Click(object sender, EventArgs e)
{
if (Alarm_Enable)
{
Alarm_Enable = false;
手动打铃ToolStripMenuItem.Enabled = true;
controlEnableToolStripMenuItem.Text = "开启闹铃";
InI.WriteIni("Alarm", "RunState", Alarm_Enable.ToString(), Config_file_Path);
SaveMessageLog("关闭闹铃");
ShowResult_message_textBox("关闭闹铃");
}
else
{
Alarm_Enable = true;
手动打铃ToolStripMenuItem.Enabled = false;
controlEnableToolStripMenuItem.Text = "关闭闹铃";
InI.WriteIni("Alarm", "RunState", Alarm_Enable.ToString(), Config_file_Path);
SaveMessageLog("开启闹铃");
ShowResult_message_textBox("开启闹铃");
}
}
private void Form1_Load(object sender, EventArgs e)
{
timer2.Stop();
myEvents = new MyEvents();
myEvents.Events += new UserEventsHandler(hezha_UserEventsHandler);
myClearEvents = new MyEvents();
myClearEvents.Events += new UserEventsHandler(ClearMessage_UserEventsHandler);
myAdjustEvents = new MyEvents();
myAdjustEvents.Events += new UserEventsHandler(AdjustTime_UserEventsHandler);
Process[] killprocess = Process.GetProcessesByName("闹铃");
if (killprocess.Length > 1)
{
MessageBox.Show("软件已运行,请勿重复打开!","闹铃");
ExitApplication = true;
Application.Exit();
}
else
{
string Alarm_Enable_State_Dis;
if (!File.Exists(Config_file_Path))
{
try
{
FileStream FS = File.Create(Config_file_Path);
FS.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
SaveMessageLog("软件已启动");
Application.Exit();
}
}
SerialPortName = InI.ReadIni("SerialPort", "PortName", "", Config_file_Path);
if (SerialPortName == string.Empty)
{
SerialPortName = "COM1";
InI.WriteIni("SerialPort", "PortName", SerialPortName, Config_file_Path);
}
serialPort1.PortName = SerialPortName;
string BaudRate_str = InI.ReadIni("SerialPort", "BaudRate", "", Config_file_Path);
if (BaudRate_str == string.Empty)
{
SerialPortBaudRate = 9600;
InI.WriteIni("SerialPort", "BaudRate", SerialPortBaudRate.ToString(), Config_file_Path);
}
else
{
SerialPortBaudRate = Convert.ToInt16(BaudRate_str);
}
SerialPortState_toolStripStatusLabel.Text = SerialPortName + " " + SerialPortBaudRate;
serialPort1.BaudRate = SerialPortBaudRate;
string Time_consumption_str = InI.ReadIni("Time_consumption", "data", "", Config_file_Path);
if (Time_consumption_str == string.Empty)
{
InI.WriteIni("Time_consumption", "data", "", Config_file_Path);
}
else
{
}
string[] Display = Time_consumption_str.Trim(';').Split(';');
textBox1.Text = Display[0];
for (int i = 1; i < Display.Length; i++)
{
textBox1.AppendText("\r\n" + Display[i]);
}
GetTime_consumption(Time_consumption_str);
string Time_consumption_Remarks_str = InI.ReadIni("Time_consumption", "remarks", "", Config_file_Path);
textBox2.Text = Time_consumption_Remarks_str.Trim(';').Replace(";", "\r\n");
string Alarm_RunState_str = InI.ReadIni("Alarm", "RunState", "", Config_file_Path);
if (Alarm_RunState_str == string.Empty)
{
Alarm_Enable = true;
InI.WriteIni("Alarm", "RunState", Alarm_Enable.ToString(), Config_file_Path);
}
else
{
Alarm_Enable = Convert.ToBoolean(Alarm_RunState_str);
}
if (Alarm_Enable)
{
controlEnableToolStripMenuItem.Text = "关闭闹铃";
手动打铃ToolStripMenuItem.Enabled = false;
Alarm_Enable_State_Dis = "闹铃开启";
}
else
{
controlEnableToolStripMenuItem.Text = "开启闹铃";
手动打铃ToolStripMenuItem.Enabled = true;
Alarm_Enable_State_Dis = "闹铃关闭";
}
MeterAddress = InI.ReadIni("Meter", "Address", "", Config_file_Path);
while (MeterAddress.Length < 12)
{
MeterAddress = "0" + MeterAddress;
}
SaveMessageLog(string.Format("软件启动 -- {0}、串口:{1}、波特率:{2}、通信地址:{3}、时段:{4}", Alarm_Enable_State_Dis, SerialPortName, SerialPortBaudRate, MeterAddress, Time_consumption_str));
}
}
//int index;
bool AdjustTimeRunState = false;
private void timer1_Tick(object sender, EventArgs e)
{
string systime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
SystemTimer_toolStripStatusLabel.Text = systime;
SaveTime1_Tick_MessageLog(systime);
}
private void 修改ToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
InI.WriteIni("Time_consumption", "data", textBox1.Text.Replace("\r",";").Replace("\n",""), Config_file_Path);
GetTime_consumption(textBox1.Text);
SaveMessageLog("修改参数: " + textBox1.Text.Replace("\r", ";").Replace("\n", ""));
}
catch