判断线程是否在规定时间内完成

这是一个WPF应用中用于监控线程是否在规定时间内完成的实现。通过创建`TimerThreadController`类,该类包含一个计时器和一个任务,可以在任务完成后或计时器触发时通知主线程。`Start`方法启动任务并设置计时器,如果任务在设定的时间内完成,将通过事件`TimerUp`发送完成状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;


namespace WpfApplication1
{
    public delegate void TimerThreadEventHandler(bool status);
    public class TimerThreadControllerParameter
    {
        public Task threadPoint;
        //public object param;
        public TimeSpan timeSpan;
    }
    public class TimerThreadController
    {
        //to run a task or a thread
        //to check the status of a task or a thread after a while and if it is done before, or not done
        private Timer controllerTimer;
        private Task threadPoint;
        public event TimerThreadEventHandler TimerUp;
        AutoResetEvent autoEvent = new AutoResetEvent(false);
        private bool timerFlag = true;
        public bool threadDone ;
        public void Start(TimerThreadControllerParameter pa)
        {
            threadPoint = pa.threadPoint;
            //threadPoint.

            pa.threadPoint.ContinueWith((t) =>
            {
                //if not ticked, run the main thread
                autoEvent.Set();
                //stop timer
                //tell main thread this task done before timer if timer is still running
                if (timerFlag)
                {
                    controllerTimer.Change(Timeout.Infinite, Timeout.Infinite);
                    threadDone = true;
                    TimerUp(threadDone);
                }
            });
            threadPoint.Start();
            controllerTimer.Change(pa.timeSpan.Seconds * 1000 + pa.timeSpan.Milliseconds, Timeout.Infinite);
            autoEvent.WaitOne();

        }
        public TimerThreadController()
        {
            controllerTimer = new Timer(timer_tick, null, Timeout.Infinite, Timeout.Infinite);
        }

        private void timer_tick(object state)
        {
            timerFlag = false;
            controllerTimer.Change(Timeout.Infinite, Timeout.Infinite);
            threadDone = false;
            TimerUp(threadDone);
            autoEvent.Set();
            
            //popup event with thread status
            //throw new NotImplementedException();

        }

    }
    //public class TimerThreadManager
    //{
    //    TimerThreadController ttc;
    //    public event TimerThreadEventHandler TimerUp;
    //    public TimerThreadManager()
    //    {
    //        ttc = new TimerThreadController();
    //    }
    //    public void StartController(TimerThreadControllerParameter pa)
    //    {
    //        Task controllThread = new Task(() => { ttc.Start(pa); });
    //        controllThread.Start();
    //        ttc.TimerUp += Ttc_TimerUp;
    //    }
    //    private void Ttc_TimerUp(bool status)
    //    {
    //        TimerUp(status);
    //    }

    //}
}


使用方法:

 private void button_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("started");
            // TimerThreadController ttc = new TimerThreadController();
            //TimerThreadManager ttc = new TimerThreadManager();
            //ttc.TimerUp += Ttc_TimerUp;

            //Thread test = new Thread(new ThreadStart(this.TestFunction));
            //TimerThreadControllerParameter ttp = new TimerThreadControllerParameter();
            //ttp.threadPoint = test;
            //ttp.param = null;
            //ttp.timeSpan = new TimeSpan(0, 0, 0, 1, 500);
            //ttc.StartController(ttp);

            object pa = null;
            Task test = new Task(() => { this.TestFunction(pa); });

            TimerThreadController ttc = new TimerThreadController();
            ttc.TimerUp += Ttc_TimerUp;
            TimerThreadControllerParameter ttp = new TimerThreadControllerParameter();
            ttp.threadPoint = test;
            //ttp.param = pa;
            ttp.timeSpan = new TimeSpan(0, 0, 0, 1, 000);
            ttc.Start(ttp);
            //justifiction by threaddone flag
            MessageBox.Show(ttc.threadDone.ToString());
            //MessageBox.Show("ended");

        }
        public void TestFunction(object obj)
        {
            Thread.Sleep(501);
        }
        private void Ttc_TimerUp(bool status)
        {
            //MessageBox.Show(status.ToString());
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cs_dnfording_0

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值