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());
}