定周期清除一个命令队列

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.执行结果

工程下载

定周期清除一个函数

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值