1.场景
一个完整的流程是a处+1,B处-1。这个完整的流程才能保证系统的运行不会有问题。但是由于一些特殊的原因,有时候a处+1执行了,B处-1却未执行。这时候怎么办了,为了应对这种特殊情况,我们需要用一个定周期函数做监控,如果B处的-1未执行,那么这个定周期函数来执行这个-1的动作。
代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using System.Windows.Forms;
namespace 时间函数测试
{
public partial class Form1 : Form
{
//定义一个时间队列
Dictionary<int, System.Threading.Timer> TimerKeys = new Dictionary<int, System.Threading.Timer>();
public Form1()
{
InitializeComponent();
}
/// <summary>
///启动函数
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
int key = int.Parse(textBox1.Text);
createCallBack(key);
}
/// <summary>
/// 终止函数
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
int key = int.Parse(textBox1.Text);
TimerKeys[key].Dispose();
TimerKeys.Remove(key);
}
/// <summary>
/// 创建回调函数
/// </summary>
/// <param name="key"></param>
private void createCallBack(int key) {
TimerCallback timerCallback = new TimerCallback(callbaick);
System.Threading.Timer myTimer = new System.Threading.Timer(callbaick,key,10000,-1);
TimerKeys.Add(key, myTimer);
}
/// <summary>
/// 回调函数
/// </summary>
/// <param name="o"></param>
private void callbaick(object o) {
MessageBox.Show("我被调用了"+o.ToString());
}
}
}
2.执行结果


被折叠的 条评论
为什么被折叠?



